首页 > 解决方案 > UITableViewCell textLabel 未覆盖单元格的整个宽度

问题描述

我尝试了所有流行的建议,例如:

tableView.cellLayoutMarginsFollowReadableWidth = false

tableView.contentInset = .zero

tableView.separatorInset = .zero

但我仍然在 textlabel 和父单元格之间得到一些间距,如下所示:

橙色是文本标签背景颜色,粉红色是单元格背景颜色

单元格创建代码如下所示:

let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = eligibleArray[indexPath.row]
cell.layoutMargins = .zero
cell.separatorInset = .zero
cell.textLabel?.textColor = .secondaryLabel
cell.textLabel?.backgroundColor = .systemOrange
cell.textLabel?.numberOfLines = 0
cell.backgroundColor = .systemPink

请帮忙提出建议

标签: swiftuitableviewuikit

解决方案


这是您可以使用默认 textLabel 单元格执行的最大值:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = .zero
    }
    if (cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins))) {
        cell.preservesSuperviewLayoutMargins = false
    }

    if (cell.responds(to: #selector(setter: UIView.layoutMargins))) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
    if tableView.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        tableView.separatorInset = .zero
    }
}

这就是结果

在此处输入图像描述

否则,您可以创建一个自定义单元格以完全控制:

class YourController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let tableView = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate =  self
    tableView.dataSource = self
    tableView.register(YourCell.self, forCellReuseIdentifier: "cellId")
    tableView.translatesAutoresizingMaskIntoConstraints = false
    
    view.addSubview(tableView)
    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 8
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! YourCell
    cell.myLabel.text = "System will compute elegibility based on the current LTV of the loanscheme selected"
    cell.myLabel.numberOfLines = 0
    cell.myLabel.backgroundColor = .orange
    cell.backgroundColor = .systemPink
    
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
 }
}

你的手机:

class YourCell: UITableViewCell {

let myLabel: UILabel = {
    let l = UILabel()
    l.backgroundColor = .orange
    l.textColor = .black
    l.numberOfLines = 0
    l.translatesAutoresizingMaskIntoConstraints = false
    
    return l
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    contentView.addSubview(myLabel)
    myLabel.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    myLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
    myLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    myLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
 }
}

这就是结果

在此处输入图像描述


推荐阅读