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

1、关于分割线的位置。ios

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

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

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

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

//设置分割线为白色

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

//UITableViewCell的分隔线格式it

        [self.tbView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];io

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

    self.tableView.sectionFooterHeight = 1.0;class

//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);

相关文章
相关标签/搜索