iOS tableView的系统分割线定格设置以及分割线自定制

一、关于分割线的位置。

    分割线的位置就是指分割线相对于tableViewCell.如果我们要根据要求调节其位置,那么在iOS7.0版本以后,提供了一个方法如下:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];

}//上、左、下、右的距离,都是CGFloat型

//设置分割线为白色

        [self.tbView setSeparatorColor:[UIColor redColor]];

//UITableViewCell的分隔线格式

        [self.tbView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

//设置组与组之间间距  不能用- (  float )tableView:( UITableView *)tableView heightForFooterInSection:(  NSInteger )section

    self.tableView.sectionFooterHeight = 1.0;

//ios8以后通过这个方法设置组间距  不能用self.tableView . sectionHeaderHeight  = 8.0

- (  CGFloat )tableView:(  UITableView *)tableView heightForHeaderInSection:( NSInteger )section

{

    return 8.0 ;

}

由于tableView是继承于scrollView,所以tableview的分割线会产生偏移,可以采用下面的方法进行设置,从而使分割线可以充满整个tableView

iOS8以后的新方法:复制即可

//系统分割线定格设置

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {

        [tableView setLayoutMargins:UIEdgeInsetsZero];

    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

}

tableViewCell 分割线自定义:首先要把cell自带的分割线给去掉,使用如下两种都行,一是把颜色设置为clearColor,二是风格设置为UITableViewCellSeparatorStyleNone。

     自定义cell分割线

   a、把自定义的分割线当成一个View放到cell的contentView上,一定要注意重用问题,所以这个view 要在cell初始化的时候添加上。示例代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

        cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"huicellacce"]];

        cell.backgroundColor = [UIColor clearColor];

        //        cell.selected = YES;

        UIImageView *imageViewSepE = [[UIImageView alloc]initWithFrame:CGRectMake(47, 49, 200, 1)];

        imageViewSepE.image = [UIImage imageNamed:@"godline"];

        [cell.contentView addSubview:imageViewSepE]; 

    }

联动

NSIndexPath *moveToIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];

    // 将右侧 tableView 移动到指定位置

    [self.tableView selectRowAtIndexPath:moveToIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

    // 取消选中效果

    [self.tableView deselectRowAtIndexPath:moveToIndexPath animated:YES];

//滑动条在左侧

tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, tableView.bounds.size.width-7);

原文地址:https://www.cnblogs.com/sunfuyou/p/5900137.html