UITableViewCell textLabel 未覆盖单元格的整个宽度
UITableViewCell textLabel not covering the full width of the cell
我尝试了所有流行的建议,例如:
tableView.cellLayoutMarginsFollowReadableWidth = 假
tableView.contentInset = .zero
tableView.separatorInset = .zero
但我仍然在文本标签和父单元格之间得到一些间距,如下所示:
单元格创建代码如下所示:
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
请大家帮忙提建议
这是您可以使用默认 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")
}
}
这是结果
我尝试了所有流行的建议,例如:
tableView.cellLayoutMarginsFollowReadableWidth = 假
tableView.contentInset = .zero
tableView.separatorInset = .zero
但我仍然在文本标签和父单元格之间得到一些间距,如下所示:
单元格创建代码如下所示:
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
请大家帮忙提建议
这是您可以使用默认 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")
}
}
这是结果