UITableView去掉最后分割线的一种方法

简介:

UITableView以style:UITableViewStylePlain方式创建时,只要有cell,就会有一条黑线 哪怕至于一个cell也会有,如图

alt

在网上找了集中方法,都不好使,比如http://blog.csdn.net/l_ch_g/article/details/9290727,中的两种方法,都尝试不好使

第一种方法

 
1、加方法
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [view release];
}
 
2、在

- (void)viewDidLoad

{

    [super viewDidLoad];

    //设置tableView不能滚动

    [self.tableView setScrollEnabled:NO];

    //在此处调用一下就可以啦 :此处假设tableView的name叫:tableView

    [self setExtraCellLineHidden:self.tableView];

}

 

在iOS4.3和iOS5.0中通过:值得注意的是在iOS4.3中可以直接设置footer为nil,但是在5.0不行,因为UITableView会默认生成一个Footer。(详见iOS Release Notes中的说明:Returning nil from the tableView:viewForHeaderInSection: method (or its footer equivalent) is no longer sufficient to hide a header. You must override tableView:heightForHeaderInSection: and return 0.0 to hide a header.)

plain类型的tableview当显示的数据很少时,下面的cell即使不显示数据也会有分割线,可以通过下面这个函数去掉多余的分割线。

 

- (void)setExtraCellLineHidden: (UITableView *)tableView

{

    UIView *view =[ [UIView alloc]init];

    view.backgroundColor = [UIColor clearColor];

    [tableView setTableFooterView:view];

    [view release];

}

当tableview的dataSource为空时,也就是没有数据可显示时,该方法无效,只能在numberOfRowsInsection函数,通过判断dataSouce的数据个数,如果为零可以将tableview的separatorStyle设置为UITableViewCellSeparatorStyleNone去掉分割线,然后在大于零时将其设置为

UITableViewCellSeparatorStyleSingleLine

 
第二种方法


if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Drawing our own separatorLine here because I need to turn it off for the
    // last row. I can only do that on the tableView and on on specific cells.
    // The y position below has to be 1 less than the cell height to keep it from
    // disappearing when the tableView is scrolled.
    UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
    separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
    separatorLine.tag = 4;

    [cell.contentView addSubview:separatorLine];

    [separatorLine release];
}

// Setup default cell setttings.
...
UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
separatorLine.hidden = NO;
...
// In the cell I want to hide the line, I just hide it.
seperatorLine.hidden = YES;
...

In viewDidLoad:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

最后,创建UITableView时,使用style:UITableViewStyleGrouped,方法解决问题,

​代码如下

1 self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
2  
3 _tableView.separatorColor = [UIColor clearColor];
4 _tableView.backgroundView=[[UIView alloc] init];    //改变表的背景视图
5 _tableView.backgroundColor = [UIColor whiteColor]; //添加颜色

使用UITableViewStyleGrouped类型创建的UITableView,背景颜色需要使用上面两个方法设置的才能生效,普通的backgroundcolor方法无效。
同时由于UITableViewStyleGrouped模式默认会有Section高度,所以,要继承下heightForHeaderInSection方法,记住在UITableViewStyleGrouped,直接修改
sectionHeaderHeight的方式是不行的。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    // This will create a "invisible" footer
    return 0.01f;
}


目录
相关文章
|
前端开发 Android开发 容器
surfaceview组件的surfaceCreated()不被调用的解决方案
原文:surfaceview组件的surfaceCreated()不被调用的解决方案 有时候我们有需要在native层做在surfaceview的上下文中做渲染,这个时候只是提供了一个单独什么都不做的surfaceview。
3972 0
|
9月前
|
机器学习/深度学习 搜索推荐 异构计算
LLM模型添加自定义Token代码示例:为Llama 3.2模型添加思考与回答标记
本文将介绍如何为大型语言模型(LLM)添加自定义token并进行训练,使模型能够有效地利用这些新增token。以Llama 3.2模型为基础,实现了类似DeepSeek R1中think和answer标记功能的扩展方法,通过监督微调使模型学习使用这些标记进行推理过程与答案输出的区分
526 0
LLM模型添加自定义Token代码示例:为Llama 3.2模型添加思考与回答标记
|
开发工具 git 开发者
如何让现有的 Git 分支跟踪远程分支?
【8月更文挑战第15天】
1411 1
如何让现有的 Git 分支跟踪远程分支?
|
Perl
解决Cocoapods重装或更新后版本不生效的问题
解决Cocoapods重装或更新后版本不生效的问题
500 1
|
存储 JavaScript 前端开发
深入理解 Git submodules
深入理解 Git submodules
788 0
深入理解 Git submodules
|
JavaScript 前端开发 Java
JavaScript高级笔记-coderwhy版本(六)
JavaScript高级笔记-coderwhy版本
261 0
C++17新特性之std::string_view
std::string_view系C++17标准发布后新增的内容,类成员变量包含两个部分:字符串指针和字符串长度,相比std::string, std::string_view涵盖了std::string的所有只读接口。如果生成的std::string无需进行修改操作,可以把std::string转换为std::string_view,std::string_view记录了对应的字符串指针和偏移位置,无需管理内存,相对std::string拥有一份字符串拷贝,如字符串查找和拷贝,效率更高。
398 0
C++17新特性之std::string_view
|
iOS开发
iOS WKWebView 打开页面空白URL为空问题解决办法
iOS WKWebView 打开页面空白URL为空问题解决办法
943 0
|
API iOS开发 Perl
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
|
开发者 iOS开发
iOS开发 - 用AFNetworking实现https单向验证,双向验证
iOS开发 - 用AFNetworking实现https单向验证,双向验证
577 0
iOS开发 - 用AFNetworking实现https单向验证,双向验证