UITableView 详尽用法

原文地址::https://blog.csdn.net/plpldog/article/details/78577042

 

 

相关文章

1、UITableViewCell详解----https://blog.csdn.net/kingsley_cxz/article/details/9151121

2、iOS开发-UITableView进阶----https://www.jianshu.com/p/6c06820c715d

3、iOS学习之UITableView----https://www.cnblogs.com/zhenzhen123/p/5071743.html

4、UITableView----https://www.jianshu.com/p/d3b1f18439e7

5、UITableView用法整理----https://www.cnblogs.com/zanglitao/p/4149560.html

6、iOS学习笔记—— UITableViewCell 详解----https://blog.csdn.net/tbl_123/article/details/38274273

 

基本 API 解释以及用法
数据源 UITableViewDataSource
代理 UITableViewDelegate
UITableViewCell
一些常用操作
UITableView 进阶
性能优化
优雅的使用 UITableView 之链式编程
UITableView 相关的开源库
MJRefresh 上拉下拉刷新组件
UITableView+FDTemplateLayoutCell 自动计算行高
SWTableViewCell Cell左右滑动操作
folding-cell 炫酷的 Cell 动画效果
基本 API 解释以及用法
数据源 UITableViewDataSource

// 返回分组数 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
// 返回每组行数 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 返回每行的单元格 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
// 返回每组头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// 返回每组尾部标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// 返回 Cell 是否在滑动时是否可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 返回 Cell 是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// TableView 右侧建立一个索引表需要的数组内容
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView; 
// 对 Cell 编辑结束后的回调
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
// 对 Cell 移动结束后的回调
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
代理 UITableViewDelegate
自定义显示效果

// Cell 即将显示,可用于自定义 Cell 显示的动画效果
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
// UITableView 的 HeaderView 即将显示,可用于自定义 HeaderView 显示的动画效果
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section;
// UITableView 的 FooterView 即将显示,可用于自定义 FooterView 显示的动画效果
-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section;
// Cell 完成显示
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath;
// HeaderView 完成显示
-(void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
// FooterView 完成显示
-(void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section;
高度设置

// 返回每个 Cell 的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 返回每个 Section 的 HeaderView 高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
// 返回每个 Section FooterView 的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// 自动计算 Cell 高度(iOS7.0 以后增加的,返回一个粗略值,系统会自动计算)
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
// 自动计算 Section 的 HeaderView 高度(iOS7.0 以后增加的)
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;
// 自动计算 Section 的 FooterView 高度(iOS7.0 以后增加的)
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;
Section Header and Footer

// 返回 Section 自定义的 HeaderView
-(nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 
// 返回 Section 自定义的 FooterView
-(nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
Section 操作

// 返回当前选中的 Row 是否高亮,一般在选择的时候才高亮
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath;
// Cell 被选中的回调
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Editing

// 返回 Cell 编辑类型
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
// 怎样编辑 Cell (iOS8.0 使用)
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;
// 设置进入编辑状态时,Cell不会缩进
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
// 开始编辑
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;
//结束编辑
-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(nullable NSIndexPath *)indexPath __TVOS_PROHIBITED;
iOS9.0 新特征———焦点

//(一些 iOS9.0 的新特征)
// 返回能否获得焦点
-(BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// 返回是否将要更新焦点
-(BOOL)tableView:(UITableView *)tableView shouldUpdateFocusInContext:(UITableViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0);
// 已经更新焦点时调用
-(void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator NS_AVAILABLE_IOS(9_0);
// 返回上一个焦点的indexPath
-(nullable NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView NS_AVAILABLE_IOS(9_0);
其他

// 单元格上的右指向按钮的响应方法回调
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
// 允许指定行移动到目标行
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
// 缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
一些使用示例

//设置右滑编辑时出现的动作
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
     NSLog(@"1111");
 }];
 topRowAction.backgroundColor = [UIColor blueColor];
 
 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
     NSLog(@"2222");
 }];
 deleteRowAction.backgroundColor =[UIColor redColor];
 return @[topRowAction, deleteRowAction];
}
UITableViewCell
3.1. 原生 UITableViewCell

UITableViewCell 自带的四种格式

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
 UITableViewCellStyleDefault,        
 UITableViewCellStyleValue1,        
 UITableViewCellStyleValue2,    
 UITableViewCellStyleSubtitle
};

60019CFD-0AE9-4509-83BB-AED61B497E0C.png
UITableViewCell右侧的 accessoryView 可以显示不同的图标,在iOS中称之为访问器,点击可以触发不同的事件,可以通过 UITableViewCellAccessoryType来设置。直接设置 accessoryView可以自定义

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
 UITableViewCellAccessoryNone,                       // 不显示任何图标
 UITableViewCellAccessoryDisclosureIndicator,    // 跳转指示图标
 UITableViewCellAccessoryDetailDisclosureButton, // 内容详情图标和跳转指示图标
 UITableViewCellAccessoryCheckmark,              // 勾选图标
 UITableViewCellAccessoryDetailButton             // 内容详情图标
};
设置 UITableViewCell 选择时的背景颜色

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];  
cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];
设置 UITableViewCell 选择时的背景

cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];  
cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];
3.2. 自定义 UITableViewCell

一些常用操作
UITableView 进阶
性能优化

关于 UITableView 的优化,网上已经有非常多的大神做出了总结,这里没有什么好说的。推荐来自优酷的ibireme大神写的这篇关于优化的文章。这估计算是比较终极的优化方案了。界面流畅度基本可以保持50 ~ 60FPS。http://blog.ibireme.com/2015/11/12/smooth_user_interfaces_for_ios/
另外,如果 Cell 的复杂度不是特别高,但高度又是不确定的。推荐使用百度团队开源的 UITableView+FDTemplateLayoutCell ,自动计算及缓存 Cell 的高度,在加快开发进度的同时,也能确保不错的流畅度。

1.1. 确保重用 Cell
1.2. 尽量减少 Cell 的视图层级,并且少用或不用透明的视图。避免离屏渲染,比如同时使用

 view.layer.masksToBounds = YES;
 view.layer.cornerRadius = 20.0;
1.3. 提前计算并缓存 Cell 的高度

// 在这里返回提前计算好的高度,效率会明显快很多。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
1.4. 在滑动的时候按需加载
关于这一点,可以看一下 VVeboTableViewDemo 的实践。
1.5. 异步绘制 Cell
1.6. 尽量不在 cellForRowAtIndexPath 方法中做过多业务处理
1.7. 减少使用 reloadData, 尽量使用下面方法来刷新列表。

//刷新指定的分组和行。
-(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
//刷新指定的分组。
-(void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
//删除时刷新指定的行数据。
-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//添加时刷新指定的行数据。
-(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
优雅的使用 UITableView 之链式编程

UITableView 相关的开源库
MJRefresh 上拉下拉刷新组件
UITableView+FDTemplateLayoutCell 自动计算行高
SWTableViewCell Cell左右滑动操作
folding-cell 炫酷的 Cell 动画效果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值