UITableView 详解

一、UITableView属性和方法

1、基础
//创建时必须制定类型
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; 
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder ;

@property (nonatomic, readonly) UITableViewStyle style;    //列表视图的类型
@property (nonatomic, weak, nullable) id  dataSource;
@property (nonatomic, weak, nullable) id  delegate;
@property (nonatomic, weak) id prefetchDataSource NS_AVAILABLE_IOS(10_0);

//高度相关
@property (nonatomic) CGFloat rowHeight;             // 行高
@property (nonatomic) CGFloat sectionHeaderHeight;   //组头高
@property (nonatomic) CGFloat sectionFooterHeight;   // 组尾高
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // 估算行高,默认0
@property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0); //估算组头高度
@property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0); //估算组尾高度

@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; //分隔线边距

@property (nonatomic, strong, nullable) UIView *backgroundView ;  //背景视图

2、数据刷新
//刷新列表
- (void)reloadData; 
//刷新section这个方法常用于新加或者删除了索引类别而无需刷新整个表视图的情况下
- (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0);
3、信息Info
@property (nonatomic, readonly) NSInteger numberOfSections;    //列表组数
//某一组有多少行
- (NSInteger)numberOfRowsInSection:(NSInteger)section;  

//某一组所占的矩形区域(包括header,footer和所有的行)
- (CGRect)rectForSection:(NSInteger)section;  
//某一组的header所占的矩形区域  
- (CGRect)rectForHeaderInSection:(NSInteger)section;
//某一组的Foote所占的矩形区域
- (CGRect)rectForFooterInSection:(NSInteger)section;
//某一分区的row所占的矩形区域
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

//点所在的分区,如果该点不在tableView的任何row上返回nil
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
//cell所在分区,如果该cell是不可见的返回nil
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell; 
//矩形区域内所有行所在的分区
- (nullable NSArray *)indexPathsForRowsInRect:(CGRect)rect; 

//返回cell,如果cell是不可见或者indexPath超出了范围则返回nil
- (nullable __kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;  //所有可见的cell
@property (nonatomic, readonly, nullable) NSArray *indexPathsForVisibleRows;    //所有可见行所在的分区

//某一组的header视图
- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
//某一组的footer视图
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

//滚动到某一位置(行)
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
//滚动到选中行
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
4、行的插入/删除/刷新
//允许多个插入/行和段被同时删除动画。可排序
- (void)beginUpdates;  
//只调用插入/删除/重载呼叫或改变一更新区块内的编辑状态。然而对于行数等属性可能是无效的

- (void)endUpdates;    
//插入某些组
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
//删除某些组
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
//刷新某些组
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
//移动组section到组newSection的位置
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);

//插入某些行
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//删除某些行
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//刷新某些分区的行
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
//移动分区indexPath的行到分区newIndexPath
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);
5、编辑
//设置编辑状态
@property (nonatomic, getter=isEditing) BOOL editing;  
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);  //是否可以选中。默认YES
@property (nonatomic) BOOL allowsSelectionDuringEditing;     // 当处在编辑模式时,是否可以选中。默认NO 
@property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);    //是否可以同时选中。默认NO
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);     //当处在编辑模式时,是否可以同时选中。默认NO
6、选中
@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow; //选中行的indexPath
@property (nonatomic, readonly, nullable) NSArray *indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0); //选中的行所在的所有indexPath

//代码手动选中与取消选中某行,注意:这两个方法将不会回调代理中的方法
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
7、外观(索引栏、分割线、Header/FooterView、Cell)
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;    //设置索引栏最小显示行数。
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor;    //设置索引栏字体颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor;    // 设置索引栏背景颜色
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor ;     // 设置索引栏被选中时的颜色

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle ;     //设置分割线的风格
@property (nonatomic, strong, nullable) UIColor *separatorColor; //设置分割线颜色
@property (nonatomic, copy, nullable) UIVisualEffect *separatorEffect; // 设置分割线毛玻璃效果(IOS8之后可用)

@property (nonatomic) BOOL cellLayoutMarginsFollowReadableWidth NS_AVAILABLE_IOS(9_0); //判断是否需要根据内容留有空白

@property (nonatomic, strong, nullable) UIView *tableHeaderView;    // 设置tableView头视图
@property (nonatomic, strong, nullable) UIView *tableFooterView;      //设置tableView尾视图

//从复用池中取cell
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  
//获取一个已注册的cell
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 
//从复用池获取头视图或尾视图
- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);  

// 通过xib文件注册cell
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
//通过oc类注册cell
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

//通过xib文件注册头视图和尾视图
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
//通过OC类注册头视图和尾视图
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
8、焦点
//是否记录最后一个焦点
@property (nonatomic) BOOL remembersLastFocusedIndexPath NS_AVAILABLE_IOS(9_0); 

二、UITableView数据源--UITableViewDataSource

@required
//返回分组数 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 返回每组行数  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional
// 返回每行的单元格 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
// 返回每组头部标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    
// 返回每尾部标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

/**编辑**/
// Cell 在滑动时是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 对 Cell 编辑结束后的回调
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

/**移动**/
// Cell是否可可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// 对 Cell 移动结束后的回调
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

/**索引栏**/
// 设置索引栏内容
- (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;                               
//返回各个索引对应的分组
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; 

三、UITableView代理--UITableViewDelegate

1、自定义显示效果
// 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;
2、高度设置
// 返回每个 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 高度
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;
// 自动计算 Section 的 FooterView 高度
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;
3、Section header & footer information
// 返回 Section 自定义的 HeaderView
-(nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 
// 返回 Section 自定义的 FooterView
-(nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
4、附件设置
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
5、选中状态设置
//设置选中的Cell 是否高亮
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
//Cell高亮时
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
//Cell取消高亮时
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

// Cell将要被选中时
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//Cell将要取消选中时
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// Cell 已选中时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//Cell已取消选中时
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
6、编辑
// 设置tableView被编辑时的状态风格,如果不设置,默认都是删除风格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
//设置删除按钮的名字
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 用于自定义创建tableView被编辑时右边的按钮,按钮类型为UITableViewRowAction。
- (nullable NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath; 
// 设置编辑时是否缩进
- (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;
7、移动特定的某行
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
8、行缩进
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath;
9、复制粘贴
//是否在指定行显示菜单,返回值为YES时,长按显示菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
//弹出选择菜单时会调用此方法(复制、粘贴、全选、剪切)
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
//选择菜单项完成之后调用此方法
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
10、焦点
// 返回能否获得焦点
-(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);

四、UITableView -UITableViewDataSourcePrefetching

五、UITableView使用示例

1、基本使用
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor grayColor];
        _tableView.estimatedRowHeight = 60;//估算行高

        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        //注册cell
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellReuseID"];
        //[_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([UITableViewCell class]) bundle:nil] forCellReuseIdentifier:@"CellReuseID"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellReuseID"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",indexPath];
    
    return cell;
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了%@",indexPath);
}
2、索引栏
// 设置索引栏内容
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H"];
}

//返回各个索引对应的分组
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}
3、编辑
//设置右滑编辑时出现的动作
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
     NSLog(@"收藏了");
 }];
 topRowAction.backgroundColor = [UIColor blueColor];

 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
     NSLog(@"删除了");
 }];
 deleteRowAction.backgroundColor =[UIColor redColor];
 return @[topRowAction, deleteRowAction];
}
4、移动
self.tableView.editing = YES;
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //移动cell前交换数据
    [self.dataArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
5、复制、粘贴
//允许 Menu菜单  
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return YES;  
}  

//每个cell都会点击出现Menu菜单  
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender  
{  
    return YES;  
}  

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender  
{  
    if (action == @selector(copy:)) {  
        [UIPasteboard generalPasteboard].string = [self.array objectAtIndex:indexPath.row];  
    }  
    if (action == @selector(cut:)) {  
        [UIPasteboard generalPasteboard].string = [self.array objectAtIndex:indexPath.row];  
        [self.array replaceObjectAtIndex:indexPath.row withObject:@""];  

        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];  
    }  

    if (action == @selector(paste:)) {  
        NSString *pasteString = [UIPasteboard generalPasteboard].string;  
        NSString *tempString = [NSString stringWithFormat:@"%@%@",[self.array objectAtIndex:indexPath.row],pasteString];  

        [self.array replaceObjectAtIndex:indexPath.row withObject:tempString];  
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];  
    }  
}
6、UITableViewCell自动计算高度(iOS8之后)
//1、设置行高自动计算
self.tableView.rowHeight = UITableViewAutomaticDimension;
//2、设置估算行高
self. tableView.estimatedRowHeight = 44;

六、UITableView性能优化

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

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

view.layer.masksToBounds = YES;
view.layer.cornerRadius = 20.0;

3、. 提前计算并缓存 Cell 的高度

// 在这里返回提前计算好的高度,效率会明显快很多。
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

4、在滑动的时候按需加载
关于这一点,可以看一下 VVeboTableViewDemo 的实践。
5、 异步绘制 Cell
6、 尽量不在 cellForRowAtIndexPath 方法中做过多业务处理
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 相关的开源库

1、MJRefresh 上拉下拉刷新组件
2、UITableView+FDTemplateLayoutCell 自动计算行高
3、SWTableViewCell Cell左右滑动操作
4、folding-cell 炫酷的 Cell 动画效果

你可能感兴趣的:(UITableView 详解)