iOS之UITableView设置全屏分隔线
来源:互联网 发布:rust golang 编辑:程序博客网 时间:2024/04/30 07:14
首先系统的分隔线有以下几种
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;-UITableViewCellSeparatorStyleNone //隐藏系统分隔线-UITableViewCellSeparatorStyleSingleLine //单分隔线-UITableViewCellSeparatorStyleSingleLineEtched //被侵蚀的单分隔线
自定义分隔线(首先要隐藏系统的分隔线)
- 通过xib或者代码在cell底部添加一条高度为1的UIView或者UILable分隔线。
- 通过drawRect:方法自绘一条分割线
// 自绘分割线- (void)drawRect:(CGRect)rect{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); CGContextFillRect(context, rect); CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0xE2/255.0f green:0xE2/255.0f blue:0xE2/255.0f alpha:1].CGColor); CGContextStrokeRect(context, CGRectMake(0, rect.size.height - 1, rect.size.width, 1));}
3.重写cell的setFrame:方法
- (void)setFrame:(CGRect)frame{ frame.size.height -= 1;//设置分隔线 //设置cell的左右间距 frame.origin.x = 5;//左间距为5 frame.size.width = [UIScreen mainScreen].bounds.size.width - 2 * frame.origin.x; // 给cellframe赋值 [super setFrame:frame];}
4.利用系统属性设置(separatorInset, layoutMargins)设置
- 对tableView的separatorInset, layoutMargins属性的设置
-(void)viewDidLoad {[super viewDidLoad];//1.调整(iOS7以上)表格分隔线边距if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { self.tableView.separatorInset = UIEdgeInsetsZero;}//2.调整(iOS8以上)view边距(或者在cell中设置preservesSuperviewLayoutMargins,二者等效)if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { self.tableView.layoutMargins = UIEdgeInsetsZero;}}
对cell的LayoutMargins属性的设置
//对cell的设置可以写在cellForRowAtIndexPath里,也可以写在willDisplayCell方法里-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"cell"; FSDiscoverSpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[FSDiscoverSpecialCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //2.调整(iOS8以上)tableView边距(与上面第2步等效,二选一即可) if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { cell.preservesSuperviewLayoutMargins = NO; } //3.调整(iOS8以上)view边距 if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } return cell;}
三种方法优缺点比较:
方法1一般使用系统的cell,或者对cell没有特殊要求的情况下使用系统的分隔线;
方法2是比较好用的,但是有些情况下系统自带的cell就足够用了,仅仅为了分隔线却还必须再自定义cell,添加一个view,设置背景颜色和frame,又显得麻烦;
方法3比较取巧,但是也需要自定义cell,在某些情况下不允许改变tableView的背景色,使用场景有限;
方法4不需要自定义cell,对系统(iOS7,iOS8以上)做个简单判断即可.
阅读全文
0 0
- iOS之UITableView设置全屏分隔线
- iOS之UITableView设置全屏分隔线
- IOS UITableView的分隔线多出问题
- ios之UITableView设置横向
- uitableview分隔线
- UITableView去掉分隔线
- IOS开发之----设置UITableView边框(
- iOS之UITableView需要设置的几个地方
- UITableView分隔线间距为0
- 去除UITableView自带分隔线
- 设置分隔线
- TableLayout设置分隔线
- IOS之相机全屏
- IOS控件之UITableView
- ios之UITableView
- IOS 之 UITableView
- IOS编程 之 UITableView
- iOS 之UITableView
- Leetcode算法学习日志-576 Out of Boundary Paths
- Swift3.0基本语法01
- Swift3.0基本语法02
- 什么是敏捷过程和XP?
- iOS之UICollectionView自定义布局
- iOS之UITableView设置全屏分隔线
- iOS之UIMenuController的简单使用
- iOS 之NSDate的Category
- Swift函数式编程之Map&Reduce&Filter
- NSAttributedString实现图文混排
- iOS9之UIStackView
- [python]python没有null
- Swift版-表情键盘展示和仿网易新闻首页左右滑动
- Swift项目创建桥接文件