IOS技术 UITableViewCell分割线无留白技巧

IOS技术 UITableViewCell分割线无留白技巧

开发工具 : Xcode 14.2
编程语言 : Swift 5
UIKit控件 : UITableView

1. UITableViewCell默认分割线

展示效果

在这里插入图片描述

实际使用中的问题

1、分割线头部有留白,逼死强迫症
2、没有内容的Cell分割线仍然存在,使用完全没有体验性可言

源码Code

    /// 考试列表
    var table =  UITableView()
    
    // MARK: --页面载入方法
    /// 页面载入方法
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        table = UITableView.init(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height))
        table.dataSource = self
        table.delegate = self
        table.separatorStyle = UITableViewCell.SeparatorStyle.singleLine
        table.allowsSelection = false
        self.view.addSubview(table)
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: nil)
        let lblExamTitle = UILabel.init(frame: CGRect(x: 45, y: 15, width: 200, height: 20))
        lblExamTitle.text = "cell\(indexPath.row)"
        cell.addSubview(lblExamTitle)
        return cell
    }
    

1. UITableViewCell调整后分割线

调整后展示效果

在这里插入图片描述

处理方式

1、补足留白
2、无内容的Cell不显示分割线

调整后源码Code

    var table =  UITableView()
    
    // MARK: --页面载入方法
    /// 页面载入方法
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        table = UITableView.init(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height))
        table.dataSource = self
        table.delegate = self
        table.separatorStyle = UITableViewCell.SeparatorStyle.singleLine
        table.separatorInset=UIEdgeInsets.zero
        table.tableFooterView = UIView(frame: CGRect.zero)
        table.allowsSelection = false
        self.view.addSubview(table)
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: nil)
        let lblExamTitle = UILabel.init(frame: CGRect(x: 45, y: 15, width: 200, height: 20))
        lblExamTitle.text = "cell\(indexPath.row)"
        cell.addSubview(lblExamTitle)
        return cell
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值