UITableView中cell线条的自定义 表格分割线(swift OC)
2017-02-15 11:57
399 查看
2.1通栏设置
通栏即UITableView的线条左右端间距都为0的情况,通栏的设置需要分别设置UITableView及UITableViewCell的layoutMargins属性第一步设置UITableView
1 | tableView.layoutMargins = UIEdgeInsetsZero |
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如何进行扁平化设置。添加扩展:12 | 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 { dd1d 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 |
12 | 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设置
新建自定义类,ILTableViewController12 | 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 } } |
版权声明:一叶原创,采用 署名-非商业性使用-相同方式共享
3.0 中国大陆 许可协议
转载:http://00red.com/blog/2016/06/22/swift-tips-uitableview-set-line/
相关文章推荐
- swift之自定义表格控件(UITableView)
- Swift_UI:(八)、UITableView自定义Cell
- Swift - UITableView里的cell底部分割线左侧靠边
- UITableView 自定义cell添加UIView作为分割线,选中消失的问题
- iOS开发-------模拟团购网站(UITableView 表格视图)以及自定义cell
- iOS-UITableView 右侧箭头+取消分割线+取消选中样式+自定义分割线(原生和自定义cell)
- Swift - UITableView里的cell底部分割线左侧靠边
- Swift编程(一):UITableView及自定义Cell的Xib
- Swift UITableView使用自定义cell
- Swift_ uitableview使用自定义(xib)cell
- iOS开发总结之项目开发中使用UITableView自定义多级表格&自定义cell
- UITableView-的cell 自定义 高度,样式
- (iphone/ipad)关于uitableview自定义cell与重用机制探究(由于文章写的太好,直接转帖备用)
- UITableView的基本使用三(自定义Cell)
- 墨珞随笔 - IOS - 自定义UITableView的cell - 李江辉
- 【UIKit】UITableView 8 自定义cell
- UITableView获取cell的row值(解决自定义cell里的button通过点击无法获得cell的indexpath的问题)
- UITableView cell自定义视图中插入…
- UITableView 隐藏空白cell的分割线
- IOS UITableView 移动自定义cell