UITableView中cell线条的自定义 表格分割线(swift OC)

2.1通栏设置

通栏即UITableView的线条左右端间距都为0的情况,通栏的设置需要分别设置UITableView及UITableViewCell的layoutMargins属性

第一步设置UITableView

1
tableView.layoutMargins = UIEdgeInsetsZero

第二步设置UITableViewCell

1
tableViewCell.layoutMargins = UIEdgeInsetsZero

2.2线条左右等间距

左右等间距,分两种情况,一种间距大于系统默认左端间距,一种为小于系统默认左端艰巨

大于系统默认左端间距时,直接设置UITableview属性

(这个地方OC同样可以设置想要的cell线条,或者通过线条的长度吧把其隐藏)

1
tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20)

小于系统默认左端间距时,则多增加一个步骤:

* 按2.1先设置成通栏
* 再按上面步骤设置UITableView的separatorInset属性

2.3Group风格扁平化

UITableView的plain style默认为扁平化风格,这里介绍group style如何进行扁平化设置。添加扩展:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
extension UITableView {
    
    private var FLAG_TABLE_VIEW_CELL_LINE: Int {
        get { return 977322 }
    }
    
    //自动添加线条
    func autoAddLineToCell(cell: UITableViewCell, indexPath: NSIndexPath, lineColor: UIColor) {
        
        let lineView = cell.viewWithTag(FLAG_TABLE_VIEW_CELL_LINE)
        if self.isNeedShow(indexPath) {
            if lineView == nil {
                self.addLineToCell(cell, lineColor: lineColor)
            }
        } else {
            lineView?.removeFromSuperview()
        }
        
    }
    
    private func addLineToCell(cell: UITableViewCell, lineColor: UIColor) {
        let view = UIView(frame: CGRectMake(0, 0, self.bounds.width, 0.5))
        view.tag = FLAG_TABLE_VIEW_CELL_LINE
        view.backgroundColor = lineColor
        cell.contentView.addSubview(view)
    }
    
    private func isNeedShow(indexPath: NSIndexPath) -> Bool {
        let countCell = self.countCell(indexPath.section)
        if countCell == 0 || countCell == 1 {
            return false
        }
        if indexPath.row == 0 {
            return false
        }
        return true
    }
    
    
    
    private func countCell(atSection: Int) -> Int {
        return self.numberOfRowsInSection(atSection)
    }
    
}
2.3.1代码设置

第一步,设置UITableView的separatorStyle属性

1
tableView.separatorStyle = .None

第二步,设置Cell

1
2
3
4
5
6
7
8
9
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(ILTableView_Cell)
    //省略逻辑...
    
    //设置
    tableView.autoAddLineToCell(cell!, indexPath: indexPath, lineColor: UIColor.lightGrayColor())
    
    return cell!
}
2.3.2 Storyboard设置

新建自定义类,ILTableViewController

1
2
3
4
5
6
7
8
9
10
11
class ILTableViewController: UITableViewController {
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
        
        //设置
        tableView.autoAddLineToCell(cell, indexPath: indexPath, lineColor: UIColor.lightGrayColor())
        
        return cell
    }
}

在storybarod中找到Controller中的class属性,设置为ILTableViewController,并修改UITableView的separatorStyleNone

版权声明:一叶原创,采用  署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议
转载:http://00red.com/blog/2016/06/22/swift-tips-uitableview-set-line/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值