要将标签固定在表视图单元格的右侧,可以使用自定义的单元格视图来实现。以下是一个可能的解决方法的代码示例:
class CustomTableViewCell: UITableViewCell {
var label: UILabel!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// 创建标签
label = UILabel(frame: CGRect(x: self.bounds.width - 100, y: 0, width: 100, height: self.bounds.height))
label.textAlignment = .right
label.font = UIFont.systemFont(ofSize: 14)
self.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
然后,在使用表视图时,使用自定义的单元格视图类:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
// 设置标签文本
cell.label.text = "右侧标签"
return cell
}
这样就可以将标签固定在表视图单元格的右侧。
上一篇:标签未更新