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

posted @   孙富有(iOS工程师)  阅读(2490)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示