关于tableView和cell的小知识点总结
2016-10-26 11:43
330 查看
1.注册cell
//自定义cell使用xib时,注册cell - (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier //重用时调用cell的方法 - (void)awakeFromNib; //自定义cell未使用xib时,注册cell - (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier //重用时调用cell的方法 - (id)initWithStyle: withReusebleCellIdentifier:
2.重用cell
//当重用cell使用dequeueReusableCellWithIdentifier:(NSString *)identifier时 //不必向tableView注册cell的Identifier //但需要判断获取得cell是否为nil - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString * tableViewIdentifier = @"tableViewCellIdentifier"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:tableViewIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier]; } } /* * 当重用cell使用dequeueReusableCellWithIdentifier:(NSString *)identifier * forIndexPath:(NSIndexPath *)indexPath时 * 必须向tableView注册cell的Identifier * 不必判断获取得cell是否为空 * runtime将使用注册时提供的资源新建一个cell并返回 */ - (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier; - (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
3.自定义cell时记得将内容添加到self.contentView而不是cell本身。
4.当cell的分割线宽度不能占满全屏宽度的时候,可以设置:
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
5.当你想去掉tableView多余的分割线的时候,可以将尾部视图设置为0达到效果
self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
6.设置cell的背景颜色
直接在cellForRow方法中设置是无效的- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;因为tableView会调整背景来改变cell的选择状态,
必须要在willDisplayCell中设置才会生效
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
7.tableView不从顶部开始加载
其实从偏移的高度就可以看出来与navigationBar有关系,有导航栏的scrollView会默认下移64高度,所以要将属性automaticallyAdjustsScrollViewInsets设置为NO就可以了,当涉及到父控制器以及子控制器时,只需初始化父控制器的该属性即可。8.tableViewCell添加侧滑删除
该功能在8.0添加新接口以后实现起来非常简单,实现代理方法即可- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewRowAction * deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.dataSourceArray removeObjectAtIndex:indexPath.row]; [self.tableView reloadData]; }]; return @[deleteAction];//注意最后要返回装有所有action的数组 }
相关文章推荐
- 关于tableview的知识总结,cell左划出现删除、置顶按钮
- 关于tableView group样式 时设置 cell的width 宽度
- Cocos2d-x学习之---关于CCTableView中reloadData()操作,删除第一个/最后一个cell后tableview位置未刷新的问题
- iOS 关于tableView cell的分割线的一些设置
- 关于UITableView的一些细碎知识点总结
- 关于错误 reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- 如何解决关于TableView里面cell随机显示的问题
- 关于tableview中cell高度不同,用代码生成cell时常出现的错误
- 关于tableview内cell自定义的注册以及创建
- 关于collectionView和tableView的两种cell的出列方法的区别
- 关于viewController中的tableview的cell在pop回来后仍有选中效果的解决方法
- 关于TableView中不止一个section的情况下首个cell的y轴不为0的会造成的问题和解决办法
- 学习IOS开发UI篇--UI知识点总结(四) UITabelView/UITableViewCell
- UICollectionViewCell 多选知识点总结
- 如何解决关于TableView里面cell随机显示的问题
- ios知识点总结——UITableView的展开与收缩及横向Table
- iOS 关于tableView cell的分割线的一些设置
- iOS 关于tableView cell的分割线的一些设置
- 关于tableview不能全部显示cell的问题?
- 如何解决关于TableView里面cell随机显示的问题