Я пытаюсь создать Uitableviewcell и кодировать ограничения. У меня возникают проблемы с получением обзоров, которые я выложил должным образом. Ниже приведен мой код. < /P>
Что я хочу, так это виды изображений в ImageStackView, чтобы быть рядом с номером. Затем сосредоточен между левым краем ячейки и вертикальным сепаратором. Я всегда изо всех сил пытался понять ограничения. < /P>
class CustomTableViewCell: UITableViewCell {
// Left Section (number label + images)
let numberLabel = UILabel()
let imageStackView = UIStackView()
let leftGroupStackView = UIStackView() // Combines numberLabel and imageStackView
// Middle Line
let verticalLine = UIView()
// Right Section (stacked labels with separator)
let topLabel = UILabel()
let bottomLabel = UILabel()
let horizontalLine = UIView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
setupConstraints()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupViews()
setupConstraints()
}
private func setupViews() {
// Configure numberLabel
numberLabel.font = .systemFont(ofSize: 16, weight: .bold)
numberLabel.textAlignment = .center
// Configure imageStackView
imageStackView.axis = .horizontal
imageStackView.spacing = 8 // Reduced spacing between images
imageStackView.alignment = .center
// Combine numberLabel and imageStackView into leftGroupStackView
leftGroupStackView.axis = .horizontal
leftGroupStackView.alignment = .center
leftGroupStackView.spacing = 8 // Reduced spacing between numberLabel and images
leftGroupStackView.addArrangedSubview(numberLabel)
leftGroupStackView.addArrangedSubview(imageStackView)
contentView.addSubview(leftGroupStackView)
// Configure verticalLine
verticalLine.backgroundColor = .lightGray
contentView.addSubview(verticalLine)
// Configure topLabel
topLabel.font = .systemFont(ofSize: 14)
topLabel.textAlignment = .right
contentView.addSubview(topLabel)
// Configure bottomLabel
bottomLabel.font = .systemFont(ofSize: 14)
bottomLabel.textAlignment = .right
contentView.addSubview(bottomLabel)
// Configure horizontalLine
horizontalLine.backgroundColor = .lightGray
contentView.addSubview(horizontalLine)
}
private func setupConstraints() {
// Disable autoresizing masks
[leftGroupStackView, verticalLine, topLabel, bottomLabel, horizontalLine].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
}
// Center the leftGroupStackView horizontally and ensure proper spacing
NSLayoutConstraint.activate([
leftGroupStackView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), // Center vertically
leftGroupStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
leftGroupStackView.trailingAnchor.constraint(lessThanOrEqualTo: verticalLine.leadingAnchor, constant: -16)
])
// Vertical line constraints
NSLayoutConstraint.activate([
verticalLine.leadingAnchor.constraint(equalTo: leftGroupStackView.trailingAnchor, constant: 16),
verticalLine.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
verticalLine.widthAnchor.constraint(equalToConstant: 1),
verticalLine.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.8)
])
// Top label constraints
NSLayoutConstraint.activate([
topLabel.leadingAnchor.constraint(equalTo: verticalLine.trailingAnchor, constant: 16),
topLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
topLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16)
])
// Horizontal line constraints
NSLayoutConstraint.activate([
horizontalLine.leadingAnchor.constraint(equalTo: topLabel.leadingAnchor),
horizontalLine.trailingAnchor.constraint(equalTo: topLabel.trailingAnchor),
horizontalLine.topAnchor.constraint(equalTo: topLabel.bottomAnchor, constant: 4),
horizontalLine.heightAnchor.constraint(equalToConstant: 1)
])
// Bottom label constraints
NSLayoutConstraint.activate([
bottomLabel.leadingAnchor.constraint(equalTo: topLabel.leadingAnchor),
bottomLabel.trailingAnchor.constraint(equalTo: topLabel.trailingAnchor),
bottomLabel.topAnchor.constraint(equalTo: horizontalLine.bottomAnchor, constant: 4),
bottomLabel.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -16)
])
}
// Update the cell with data
func configure(number: Int, images: [UIImage], topText: String, bottomText: String) {
numberLabel.text = "\(number)"
topLabel.text = topText
bottomLabel.text = bottomText
// Remove existing image views
imageStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
// Add new image views with larger size
for image in images {
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
imageView.widthAnchor.constraint(equalToConstant: 36).isActive = true // Slightly larger size
imageView.heightAnchor.constraint(equalToConstant: 36).isActive = true // Slightly larger size
imageStackView.addArrangedSubview(imageView)
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... contraints
Проблема макета с ограничениями UITableViewCell ⇐ IOS
Программируем под IOS
-
Anonymous
1737816016
Anonymous
Я пытаюсь создать Uitableviewcell и кодировать ограничения. У меня возникают проблемы с получением обзоров, которые я выложил должным образом. Ниже приведен мой код. < /P>
Что я хочу, так это виды изображений в ImageStackView, чтобы быть рядом с номером. Затем сосредоточен между левым краем ячейки и вертикальным сепаратором. Я всегда изо всех сил пытался понять ограничения. < /P>
class CustomTableViewCell: UITableViewCell {
// Left Section (number label + images)
let numberLabel = UILabel()
let imageStackView = UIStackView()
let leftGroupStackView = UIStackView() // Combines numberLabel and imageStackView
// Middle Line
let verticalLine = UIView()
// Right Section (stacked labels with separator)
let topLabel = UILabel()
let bottomLabel = UILabel()
let horizontalLine = UIView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
setupConstraints()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupViews()
setupConstraints()
}
private func setupViews() {
// Configure numberLabel
numberLabel.font = .systemFont(ofSize: 16, weight: .bold)
numberLabel.textAlignment = .center
// Configure imageStackView
imageStackView.axis = .horizontal
imageStackView.spacing = 8 // Reduced spacing between images
imageStackView.alignment = .center
// Combine numberLabel and imageStackView into leftGroupStackView
leftGroupStackView.axis = .horizontal
leftGroupStackView.alignment = .center
leftGroupStackView.spacing = 8 // Reduced spacing between numberLabel and images
leftGroupStackView.addArrangedSubview(numberLabel)
leftGroupStackView.addArrangedSubview(imageStackView)
contentView.addSubview(leftGroupStackView)
// Configure verticalLine
verticalLine.backgroundColor = .lightGray
contentView.addSubview(verticalLine)
// Configure topLabel
topLabel.font = .systemFont(ofSize: 14)
topLabel.textAlignment = .right
contentView.addSubview(topLabel)
// Configure bottomLabel
bottomLabel.font = .systemFont(ofSize: 14)
bottomLabel.textAlignment = .right
contentView.addSubview(bottomLabel)
// Configure horizontalLine
horizontalLine.backgroundColor = .lightGray
contentView.addSubview(horizontalLine)
}
private func setupConstraints() {
// Disable autoresizing masks
[leftGroupStackView, verticalLine, topLabel, bottomLabel, horizontalLine].forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
}
// Center the leftGroupStackView horizontally and ensure proper spacing
NSLayoutConstraint.activate([
leftGroupStackView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), // Center vertically
leftGroupStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
leftGroupStackView.trailingAnchor.constraint(lessThanOrEqualTo: verticalLine.leadingAnchor, constant: -16)
])
// Vertical line constraints
NSLayoutConstraint.activate([
verticalLine.leadingAnchor.constraint(equalTo: leftGroupStackView.trailingAnchor, constant: 16),
verticalLine.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
verticalLine.widthAnchor.constraint(equalToConstant: 1),
verticalLine.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.8)
])
// Top label constraints
NSLayoutConstraint.activate([
topLabel.leadingAnchor.constraint(equalTo: verticalLine.trailingAnchor, constant: 16),
topLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
topLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 16)
])
// Horizontal line constraints
NSLayoutConstraint.activate([
horizontalLine.leadingAnchor.constraint(equalTo: topLabel.leadingAnchor),
horizontalLine.trailingAnchor.constraint(equalTo: topLabel.trailingAnchor),
horizontalLine.topAnchor.constraint(equalTo: topLabel.bottomAnchor, constant: 4),
horizontalLine.heightAnchor.constraint(equalToConstant: 1)
])
// Bottom label constraints
NSLayoutConstraint.activate([
bottomLabel.leadingAnchor.constraint(equalTo: topLabel.leadingAnchor),
bottomLabel.trailingAnchor.constraint(equalTo: topLabel.trailingAnchor),
bottomLabel.topAnchor.constraint(equalTo: horizontalLine.bottomAnchor, constant: 4),
bottomLabel.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -16)
])
}
// Update the cell with data
func configure(number: Int, images: [UIImage], topText: String, bottomText: String) {
numberLabel.text = "\(number)"
topLabel.text = topText
bottomLabel.text = bottomText
// Remove existing image views
imageStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
// Add new image views with larger size
for image in images {
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
imageView.widthAnchor.constraint(equalToConstant: 36).isActive = true // Slightly larger size
imageView.heightAnchor.constraint(equalToConstant: 36).isActive = true // Slightly larger size
imageStackView.addArrangedSubview(imageView)
}
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79385741/layout-problem-with-uitableviewcell-contraints[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия