swift开发UITableViewCell的分割线与屏幕边缘对齐显示的解决方案 —— Swift

swift开发UITableViewCell的分割线与屏幕边缘对齐显示的解决方案

 

iOS开发中我们使用UITableView控件时会发现每个UITableviewCell的分割线左侧距离屏幕有一定的间距,这样的默认效果也很好,但是往往我们会遇到让把cell的分割线与屏幕的左右边缘对齐就是让分割线的宽度和屏幕的宽度相同的显示效果。

 

介绍两种实现的方法:

第一种方法:

可以通过在cell中自定义一个cell底部subView的方法,使得subView的左右约束与屏幕的左右间距为0来实现,此种方法就不做重点说明。

 

第二种方法:

通过实现UITableViewDelegate中的代理方法来实现

 

optional public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)

 

具体实现如下代码:

//分割线从左端顶部显示(使cell的)分割线与屏幕的左右两端对齐显示
    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: UITableViewCell.layoutMargins))){
            
            cell.layoutMargins = .zero
        }
    }

 

原文地址:https://www.cnblogs.com/Rong-Shengcom/p/9309862.html