【发布时间】:2020-01-07 04:58:25
【问题描述】:
我的问题是我想在表格单元格中写入长标签,但我找不到任何修复方法。我试过 numberOfLines = 0, UITableView.automatic Dimension 代码,但这些都不能解决我的问题。 单击时我的 tableView 单元格将展开,并且会出现其他标签,即 cevap 标签。展开的东西正常工作,但标签无法放入屏幕。这是我的锚代码(我想我对此有疑问,但大约 2-3 天我找不到任何解决方案。)
class expandingCell: UITableViewCell {
let cellView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.clear
return view
}()
let soruLabel: UILabel = {
let sorulabel = UILabel()
sorulabel.textColor = .black
sorulabel.font = UIFont.boldSystemFont(ofSize: 18)
return sorulabel
}()
let cevapLabel: UILabel = {
let cevaplabel = UILabel()
cevaplabel.textColor = .black
cevaplabel.font = UIFont.boldSystemFont(ofSize: 18)
return cevaplabel
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
func set(content: expandingTableCellForsss) {
self.soruLabel.text = content.soru
self.cevapLabel.text = content.expanded ? content.cevap : ""
}
func setup() {
backgroundColor = UIColor.init(red: 245, green: 245, blue: 245, alpha: 1)
addSubview(cellView)
cellView.addSubview(soruLabel)
cellView.addSubview(cevapLabel)
cellView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8, width: frame.width, height: 60)
soruLabel.anchor(top: cellView.topAnchor, left: cellView.leftAnchor, bottom: nil, right: cellView.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 35, height: 35)
cevapLabel.anchor(top: soruLabel.bottomAnchor, left: cellView.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 0, height: 35)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我用于扩展的自定义单元格。 我没有使用故事板,所以我必须使用编码进行布局。如果您能帮助我,我将不胜感激。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "details2", for: indexPath) as! customCell
let detailGelen = detailsModel[indexPath.row]
cell.detailCellLabel.text = detailGelen.itemDetailName
cell.priceLabel.text = detailGelen.itemDetailPrice!
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: expandingTableCellForsss.self), for: indexPath) as! expandingCell
cell.set(content: dataSource[indexPath.row])
return cell
}
}
此代码用于不展开部分 == 1 和
的单元格 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//let detailGelen = detailsModel[indexPath.row] BURADAN DATAYI ALICAK
if indexPath.section == 0 {
let cell = tableView.cellForRow(at: indexPath) as! customCell
cell.count += 1
cell.countLabel.text = "x \(cell.count)"
cell.imageForCell.image = nil
}else {
print("naptın")
let content = dataSource[indexPath.row]
content.expanded = !content.expanded
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
出现了 didSelectItem 和扩展单元格,但不完全是我希望的。
最后的情况是:
【问题讨论】:
-
您的问题对我来说不是很清楚,您是在询问具有动态标签高度的表格视图单元格的自动调整大小吗?如果不是这样,如果您添加更多关于 tableView 单元格的代码将在单击时展开并且其他标签会出现 cevap 标签会很好..
-
我试图更清楚我编辑了我的问题。我希望会很清楚。
-
所以问题出在扩展单元格上?您想根据文字确定高度吗?
-
是的,我们可以这么说
标签: swift xcode uitableview tableview expandable