手机
当前位置:查字典教程网 >编程开发 >IOS开发 >iOS应用开发中UITableView的分割线的一些设置技巧
iOS应用开发中UITableView的分割线的一些设置技巧
摘要:对于ios7,ios8及以上来说,调整UITableView的cell的分割线位置已经是相当不便,因为UITableView内部使用了mar...

对于ios7,ios8及以上来说,调整UITableView的cell的分割线位置已经是相当不便,因为UITableView内部使用了margin layout.

其实只需要如下这样子就可以实现分割线的控制。

复制代码 代码如下:

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

{

// 下面这几行代码是用来设置cell的上下行线的位置

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

[cell setLayoutMargins:UIEdgeInsetsZero];

}

//按照作者最后的意思还要加上下面这一段,才能做到底部线控制位置,所以这里按stackflow上的做法添加上吧。

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

[cell setPreservesSuperviewLayoutMargins:NO];

}

}

如果要直接使用TableView的sectionTitle,但又想设置它的字体,颜色什么的,可以使用如下方法。

复制代码 代码如下:

- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section

{

// Background color

view.tintColor = [UIColor blueColor];

// Text Color

UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;

[header.textLabel setTextColor:[UIColor redColor]];

// 另一种方法设置背景颜色

// header.contentView.backgroundColor = [UIColor blackColor];

}

不显示分割线

通过tableFooterView修改UITableView分割线:

在使用UITableView的时候,如果没有数据/数据很少,会发现即使没有数据的cell也会有分割线,这样看起来并不美观,通常我们希望只有显示数据的cell会显示对应的分割线,而不显示数据的cell不显示分割线。

常用的做法有两种:

第一种做法是首先取消显示分割线,然后自定义cell,在cell的最底部加上一个高度为1的view,这样看起来就像是一条分割线。只有cell有数据显示出来的时候才会显示这个view,这样就达到了目的。

第二种做法既不用取消显示分割线,也不需要自定义cell,而是直接这样做:

复制代码 代码如下:

self.tableView.tableFooterView = [[UIView alloc] init];

运行显示结果,发现就已经达到了我们的目的。很明显这种做法更方便。

【iOS应用开发中UITableView的分割线的一些设置技巧】相关文章:

iOS开发中CAlayer层的属性以及自定义层的方法

IOS开发:Xcode7中的一些调试技巧

以代码实例总结iOS应用开发中数据的存储方式

讲解iOS开发中UITableView列表设计的基本要点

iOS开发中最有用关键的代码整合

iOS应用开发中UITabBarController标签栏控制器使用进阶

iOS开发中使用UIScrollView实现图片轮播和点击加载

iOS应用开发中StoryBoard搭建UI界面的基本使用讲解

iOS开发中UITabBarController的使用示例

iOS开发中UITableview控件的基本使用及性能优化方法

精品推荐
分类导航