iOS之UITableView设置全屏分隔线

来源:互联网 发布:零基础云计算视频教程 编辑:程序博客网 时间:2024/04/30 04:33

首先系统的分隔线有以下几种

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;-UITableViewCellSeparatorStyleNone //隐藏系统分隔线-UITableViewCellSeparatorStyleSingleLine //单分隔线-UITableViewCellSeparatorStyleSingleLineEtched //被侵蚀的单分隔线

自定义分隔线(首先要隐藏系统的分隔线)

  1. 通过xib或者代码在cell底部添加一条高度为1的UIView或者UILable分隔线。
  2. 通过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以上)做个简单判断即可.

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾驶证明过期了怎么办 驾驶证年过期了怎么办 驾照过期六个月怎么办 移动预约号码取消怎么办 身份证换地址驾驶证怎么办 刚来成都怎么办居住证 我在外地怎么办身份证 身份证丢在外地怎么办 换领新身份证时旧证丢了怎么办 二代身份证重号怎么办 北京行驶证到期怎么办 北京驾驶证即将过期怎么办 去澳门没有网络怎么办 三个周期未年检怎么办 深圳驾照丢了怎么办 武汉驾照年审过期怎么办 武汉驾照过期了怎么办 科二过不了怎么办 南京身份证到期换新怎么办 过期身份证丢了怎么办 南京驾照过期了怎么办 换驾照身体证明怎么办 学车办理暂住证怎么办 a牌驾照扣分怎么办 b牌驾照扣分怎么办 b驾照扣分了怎么办 考驾照要暂住证怎么办 换驾驶证有色盲怎么办 外籍人员办理就业证怎么办 驾驶证该审过期怎么办 小车证扣满12分怎么办 b证扣满12分怎么办 车过户后保险怎么办 换新轮胎旧轮胎怎么办 驾照一审超一年怎么办 驾驶证年检过期了怎么办 交管12123怎么办进京证 驾照登录密码忘记怎么办 预约密码忘记了怎么办 学驾照密码忘记怎么办 考驾照不识字怎么办