修改UITableViewCell中textLabel偏移量的几种方式

本文通过创建一个简单的UITableViewDemo,探讨了修改UITableViewCell内textLabel偏移量的四种方法:1) 使用indentationLevel和indentationWidth属性;2) 子类化UITableViewCell并重写layoutSubviews;3) 完全自定义UITableViewCell;4) 重新设置textLabel的约束。详细展示了每种方法的效果和代码实现。
摘要由CSDN通过智能技术生成

首先,我们创建一个简单的demo,在Main.storyboard中为ViewController添加一个导航控制器(Editor - Embed in - Navigation Controller,在ViewController中添加一个UITableView并添加约束,全屏显示,设置好相应的代理。在ViewController中实现部分代理方法,实现显示50cell,页面效果如下:


代码如下:

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var dataSource = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        for i in 0..<50 {
            dataSource.append("第\(i)个cell")
        }
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
            cell?.accessoryType = .disclosureIndicator
        }
        cell?.textLabel?.text = dataSource[indexPath.row]
        cell?.detailTextLabel?.text = dataSource[indexPath.row]
        return cell!
    }
}

现在我想修改textLabeldetailTextLabel等显示内容的位置,右移10或者20

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值