UITableView使用总结
来源:互联网 发布:碳酸钙 柠檬酸钙 知乎 编辑:程序博客网 时间:2024/05/22 13:02
1、UITableView有两种样式 UITableViewStyleGrouped和 UITableViewStylePlain
2、tableV.dataSource = self; 数据源协议的几个方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
对于UITableViewStyleGrouped风格的TableView指定有多少组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
指定第section组有多少行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
为indexPath.section组的indexPath.row行返回一个UITableViewCell
为section添加头部标题或者尾部标题:
#pragma mark - 为section添加标题的方法
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return @"头部标题";}- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"尾部标题";}
当tableView加载时,会自动调用这三个方法,从数据源获取需要显示多少组、多少行、每一行需要显示什么内容。
3、UITableViewCell中已经封装了很多控件属性:
4、TableView的索引
#pragma mark 返回表格右边的索引条
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return @[@"a",@"b",@"c",@"d"];}
在屏幕右侧点击第几个索引,则跳到第几个section。
如果要用拼音索引,需要引用第三方库Pinyin4Objc
5、TableViewCell的Style
UITableViewCell *tc = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
UITableViewCellStyleDefault:不显示detailTextLabel
UITableViewCellStyleSubtitle:在底部显示detailTextLabel
UITableViewCellStyleValue1:在右边显示detailTextLabel
UITableViewCellStyleValue2:不显示图片,显示detailTextLabel
6、设置每一行的高度:
#pragma mark 设置高度- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 70;}
7、TableViewCell的accessoryType使用系统的枚举类型设置cell最右边的小控件。accessoryView则可以设置为自定义的view
8、刷新tableView
//1、更改模型数据 //2、重新向数据源索取数据,重新调用数据源的方法,根据返回值显示数据 [_tableView reloadData]; //刷新局部行 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0]; NSArray *temp = @[indexPath]; [_tableView reloadRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationMiddle];
通过修改withRowAnimation可以调用不同的刷新动画
9、点击tableView时,可以将点击的行号绑定到alertView的tag属性中:
//将行号绑定到alert alert.tag = indexPath.row; [alert show];
10、TableView显示多少行,cellForRowAtIndexPath就被调用多少次,创建多少行cell。
11、性能优化:从缓存池中获取cell:
static NSString *ID = @"C1"; //返回每一行显示内容 //从缓存池中取出可循环利用的cell UITableViewCell *tc = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ID]; //如果缓存池中没有可循环利用的cell if (!tc) { tc = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; }
12、每个cell都有一个选中状态
代理方法didSelectRowAtIndexPath监听选中
代理方法didSelectRowAtIndexPathj监听取消选中
代理方法willSelectRowAtIndexPath在选中之前调用
13、TableViewCell默认点击是选中,可以在点击方法中调用如下方法来取消选中。
14、通过点击来改变Cell状态是不对的,因为在循环利用Cell的时候,会影响到其他Cell的现实,需要通过模型来控制Cell的状态。
15、当我调用deleteRowsAtIndexPaths从TableView中删除动画并展现动画效果时,仍然需要将对应的数据删掉,即TableView认为要显示的行数与实际的数据量要相等。
[_shopArray removeObjectsInArray:deleteArray]; //修改模型数据[_tableView deleteRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationFade]; //更新界面
17、tableView的编辑模式以及监听删除点击
#pragma mark 提交编辑操作//只要实现了这个方法,就自动实现了左滑出现删除按钮- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //如果是删除操作 if (editingStyle == UITableViewCellEditingStyleDelete) { //当用户提交编辑操作时调用 ,比如在编辑模式下点击了删除按钮 //删除模型数据 Person *p = _personArray[indexPath.row]; [_personArray removeObject:p]; //从Table View 中删除 [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }}- (IBAction)remove:(UIBarButtonItem *)sender { //设置编辑模式 BOOL edit = !_tableView.editing; [_tableView setEditing:edit animated:YES];}@end
18、编辑模式有两种模式,一种是删除,一种是添加,无论是哪种模式,当点击时都会进入tableView:commitEditingStyle:forRowAtIndexPath: 方法,可以通过commitEditingStyle参数来判断是哪种模式
0 0
- UITableView 使用总结
- UITableView使用总结
- UITableView的使用总结
- UITableView使用总结
- IOS UITableView 使用总结
- UITableView控件使用总结
- UItableView 的使用总结
- UITableView使用总结
- iOS UITableView 使用总结
- IOS中UITableView使用总结
- IOS中UITableView使用总结
- UITableView性能优化及使用总结
- UITableView使用总结和性能优化
- 关于UItableView的使用代码总结
- iOS中UITableView中使用总结
- UITableView使用总结和性能优化
- UITableView总结
- UITableView总结
- Uva 11044 Searching for Nessy
- bool判断回文数2
- Centos6.5内核编译(3.14.13)
- Java容器类Collection、List、ArrayList、Vector及map、HashTable、HashMap区别
- MessageFormat用法
- UITableView使用总结
- Drupal 默认安装的目录结构
- 视频图片人脸检测 (III)【加注释】
- hdu 1256 画 8
- gitblit git SERVER window 安装配置 hook post-receive 自动部署
- log4j配置祥解
- sqlite 数据类型
- iOS中如何将UIDatePicker的时间转换成24小时制
- PLSQL连接Oracle11g64位操作步骤