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

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

在网上找了集中方法,都不好使,比如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;
}
时间: 2024-09-03 03:37:16

UITableView去掉最后分割线的一种方法的相关文章

ASP.NET DataTable去掉重复行的2种方法

 这篇文章主要介绍了ASP.NET DataTable去掉重复行的2种方法,本文直接给出去重代码,需要的朋友可以参考下     第一种,使用Linq查询表达式,code如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 DataTable testtable = new DataTable(); testtable.Columns.Add("ID"); testtable.Columns.Add("ProductName&quo

Android中去掉标题栏的几种方法(三种)_Android

1.在java代码中 (SplashActivity继承AppCompatActivity时无效) 2.在manifest.xml中改Theme 3.先在style.xml中自定义style <?xml version="1.0" encoding="UTF-8" ?> <resources> <style name="notitle"> <item name="android:windowNo

Android开发之去掉标题栏的三种方法,推荐第三种

Android:去掉标题栏的三种方法和全屏的三种方法 第一种:一般入门的时候经常使用的一种方法 onCreate函数中加入以下代码: requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 注意这句一定要写在setContentView()方法的前面,不然会报错的 第二种:在AndroidManifest.xml文件中定义 <application android:icon="@drawable/icon" android:la

syntaxhighlighter 去掉右上角问号图标的三种方法_网页编辑器

使用免费产品就有帮它宣传的义务,所以,在使用该工具时,会自动显示一个帮助图标,点击它会提示访问官方网站并呼吁捐款,付费后可以去掉该图标. syntaxhighlighter       官方的做法无可非议,不过如果我们想取消这个看起来比较碍眼的图标,又不想付费,那有什么办法呢?如下是我获知的三种方法:       方法一       在网页前面,通常是在<head></head>里加上如下代码: <script type="text/javascript"

Android中去掉标题栏的几种方法(三种)

1.在java代码中 (SplashActivity继承AppCompatActivity时无效) 2.在manifest.xml中改Theme 3.先在style.xml中自定义style <?xml version="1.0" encoding="UTF-8" ?> <resources> <style name="notitle"> <item name="android:windowNo

Android 关于ExpandableListView去掉里头分割线的方法

关于ExpandableListView, 自己写了个类继承自BaseExpandableListAdapter groups,childs 都弄好了,显示出来的效果跟网上很多demo一样,我现在就是想去掉那个组下面各item间的分割线 有知道的么? ------解决方案-------------------- expandableList.setDivider(null); up,还不行就设置一个透明的颜色. ------解决方案-------------------- 可以的, androi

在Word文档中插入Cad图的3种方法

在word文件中插入我们绘制的cad图,方法大体上有三种,但是它们达到的效果却并不相同,有的显得精致些,有的显得粗糙些,根据我们写作的目的还要进行相应的再处理,这个过程有一些技巧,现在把它写下来,希望能给读者朋友以启发. 要在word文件中插入一个cad图,可以利用下面的三种方法来实现: 第一种,利用键盘上的"print screen sysrq"来抓取cad图,再通过系统自带的画图软件做进一步的修剪,去掉cad剪贴图中多余的部分,这项工作为了是能使图片在word文件中显示的足够的清晰

[原创]防止网站被采集最有效的三种方法

网站|原创|防采集 总结了一下,有三种实用的方法. 1.文章头尾加随机广告..2.文章列表加随机不同的链接标签,比如<a href="",<a class="dds" href=''3.正文头尾或列表头尾添加<!--重复特征代码--> 第一种防采集方法:下面我详细说一下这三种方法的实际应用: 如果全加上,绝对可以有效的防采集,单独加一种就可以让采集者头疼..完全可以对付通用的CMS采集程序.. 在采集时,通常都是指定头尾特征从哪到哪过滤.这里

PHP中对用户身份认证实现两种方法

当访问者浏览受保护页面时,客户端浏览器会弹出对话窗口要求用户输入用户名和密码,对用户的身份进行验证,以决定用户是否有权访问页面.下面用两种方法来说明其实现原理. 一.用HTTP标头来实现 标头是服务器以HTTP协议传送HTML信息到浏览器前所送出的字串.HTTP采用一种挑战/响应模式对试图进入受密码保护区域的用户进行身份验证.具体来说,当用户首次向WEB服务器发出访问受保护区域的请求时,挑战进程被启动,服务器返回特殊的401标头,表明该用户身份未经验证.客户端浏览器在检测到上述响应之后自动弹出对