您的位置:首页 > 产品设计 > UI/UE

UITableView 零散知识点总结

2016-05-04 00:15 387 查看
tableView行高有关:http://blog.sunnyxx.com/2015/05/17/cell-height-calculation/

0.在已经获取到数据源的情况下,设置tableview默认选中行和点击默认行所执行的操作

//设置默认选中第一行,且滚到最上面
[self.listView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];

//设置执行点击默认行会进行的操作
[self tableView:self.listView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];


1.设置tableView的cell的分割线的样式

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


2.tableViewCell简单粗暴的创建方式:

+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *ID = @"CELL";

UITableViewCell *cell = [tableView dequeReuseableCellWithIdentifier:ID];

if(nil == cell){
cell = [[UITableView alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];

}
return cell;
}


3.tableViewCell的编辑:增加-修改-删除,都要先修改数据源 的数据,然后执行下列对应的方法即可实现

1. 只修改数据源中的数据, 数量并没有改变
[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

2. 数据减少
[_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

3. 数据增加
[_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];


4.实现cell的滑动删除删除

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;


5.防止headerView盖住第一个cell的方法:

设置header的高度为20---heightForHeaderInSection

6.tableView:要通过代理方法返回一个headerView,首先需要通过代理方法设置一个组头的高度,要不然就不会调用返回headerView的代理方法

7.tableView 自动计算行高:一般只用于cell的内容是固定的,如果有些控件是代码动态添加的 则一般不用这种方式,IOS8之后才有这个功能

self.tableView.rowHeight = UITableViewAutomaticDimension;//设置行高为自动计算模式
self.tableView.estimatedRowHeight = 50;//给定一个预估行高值

// 设置label每一行文字的最大宽度
// 为了保证计算出来的数值 跟 真正显示出来的效果 一致
self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;

// 内容设置完强制布局
[self layoutIfNeeded];


8.在有导航控制器的时候,设置tableView的frame的时候要注意,当你Y值设为0的时候,其实默认内部会帮我们做处理.实际显示到界面上的效果Y值其实是64

9.使用tableViewController的时候,要想改变tableView的style,首先是初始化的时候指定,另外可以重写initWithStyle:方法

- (instancetype)initWithStyle:(UITableViewStyle)style{

if (self = [super initWithStyle:UITableViewStyleXXX]) {

}

return self;
}


10.使用tableView,style为gruped的时候,会发现组头的高度和实际不一致,解决的办法是给它一个设定一个组尾值(不为0,可设0.1)

11.tableView 的cell的便利构造器的设置:cellConfig

1.设置一个类CellConfig,继承自NSobject,里面设置上一个创建一个cell所需的基本属性,这样外界拿到这个类对象就可以有足够的信息去创建一个cell.属性包括:

className---cell的类名

title----cell的标题

height---cell的高度

subTitle--子标题

cellType---cell的类型

remark---预留属性

2.提供一个类方法给外界创建类对象

+ (instancetype)cellConfigWithName:(NSString *)className title:(NSString *)title height:(CGFloat)height cellType:(NSString *)cellType;

- 3 然后写一个类,继承自UITableViewCell,里面设置一个 cellConfig的属性,重写其set方法,当外界将cellConfig的模型对象传给他的时候,返回一个自定义cell.

tableviewCell的分割线设置思路:

1.加空白view,设置背景色

2.设置cell的contentView的frame,设置cell的背景色,高度控制好 就会有分割线的效果

3.直接设置cell的frame,会发现在外面直接设置cell的frame都是无效的,可以通过重写setframe,拦截系统的对frame的设置.

注意点:区别dequeueReusableCellWithIdentifier 和dequeueReusableCellWithIdentifier: forIndexPath;;;

带indexPath的是比较新的方法,在ios6.0之后才可用,且需要结合注册一起使用

如果是按照老式的写法,如果是nil,就自己实例化cell,那就必须用不带 indexPath的.

reloadData相关

相信很多人会遇到这种情况,当tableView正在滚动的时候,如果reloadData,偶尔发生App crash的情况。 这种情况有时候有,有时候没有,已经难倒了很多人。直至今天,我在stackoverflow上面,仍没有发现真正有说到其本质的帖子。我的处女贴,选择这个问题来阐述一下我的观点。

第一句话,阐述问题的本质:在tableView的dataSource被改变 和 tableView的reloadData被调用之间有个时间差,而正是在这个期间,tableView的delegate方法被调用,如果新的dataSource的count小于原来的dataSource count,crash就很有可能发生了。

下面的笔记提供了两种解决方案,和记录了一个典型的错误,即 在background thread中修改了datasource,虽然调用 [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nilwaitUntilDone:NO];

记住正确的原则: Always change the dataSource and(注意这个and) reloadData in the mainThread. What's more, reloadData should be called immediately after the dataSource change. If dataSource is changed but tableView's reloadData method is not called immediately, the tableView
may crash if it's in scrolling. Crash Reason: There is still a time gap between the dataSource change and reloadData. If the table is scrolling during the time gap, the app may Crash!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios UI