UITableView 詳解

一、UITableView屬性和方法

1、基礎(chǔ)
//創(chuàng)建時必須制定類型
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; 
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder ;

@property (nonatomic, readonly) UITableViewStyle style;    //列表視圖的類型
@property (nonatomic, weak, nullable) id <UITableViewDataSource> dataSource;
@property (nonatomic, weak, nullable) id <UITableViewDelegate> delegate;
@property (nonatomic, weak) id<UITableViewDataSourcePrefetching> prefetchDataSource NS_AVAILABLE_IOS(10_0);

//高度相關(guān)
@property (nonatomic) CGFloat rowHeight;             // 行高
@property (nonatomic) CGFloat sectionHeaderHeight;   //組頭高
@property (nonatomic) CGFloat sectionFooterHeight;   // 組尾高
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // 估算行高,默認(rèn)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、數(shù)據(jù)刷新
//刷新列表
- (void)reloadData; 
//刷新section這個方法常用于新加或者刪除了索引類別而無需刷新整個表視圖的情況下
- (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0);
3、信息Info
@property (nonatomic, readonly) NSInteger numberOfSections;    //列表組數(shù)
//某一組有多少行
- (NSInteger)numberOfRowsInSection:(NSInteger)section;  

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

//點所在的分區(qū),如果該點不在tableView的任何row上返回nil
- (nullable NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
//cell所在分區(qū),如果該cell是不可見的返回nil
- (nullable NSIndexPath *)indexPathForCell:(UITableViewCell *)cell; 
//矩形區(qū)域內(nèi)所有行所在的分區(qū)
- (nullable NSArray<NSIndexPath *> *)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<NSIndexPath *> *indexPathsForVisibleRows;    //所有可見行所在的分區(qū)

//某一組的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;  
//只調(diào)用插入/刪除/重載呼叫或改變一更新區(qū)塊內(nèi)的編輯狀態(tài)。然而對于行數(shù)等屬性可能是無效的

- (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<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//刪除某些行
- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//刷新某些分區(qū)的行
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
//移動分區(qū)indexPath的行到分區(qū)newIndexPath
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);
5、編輯
//設(shè)置編輯狀態(tài)
@property (nonatomic, getter=isEditing) BOOL editing;  
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);  //是否可以選中。默認(rèn)YES
@property (nonatomic) BOOL allowsSelectionDuringEditing;     // 當(dāng)處在編輯模式時,是否可以選中。默認(rèn)NO 
@property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);    //是否可以同時選中。默認(rèn)NO
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);     //當(dāng)處在編輯模式時,是否可以同時選中。默認(rèn)NO
6、選中
@property (nonatomic, readonly, nullable) NSIndexPath *indexPathForSelectedRow; //選中行的indexPath
@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0); //選中的行所在的所有indexPath

//代碼手動選中與取消選中某行,注意:這兩個方法將不會回調(diào)代理中的方法
- (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;    //設(shè)置索引欄最小顯示行數(shù)。
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor;    //設(shè)置索引欄字體顏色
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor;    // 設(shè)置索引欄背景顏色
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor ;     // 設(shè)置索引欄被選中時的顏色

@property (nonatomic) UITableViewCellSeparatorStyle separatorStyle ;     //設(shè)置分割線的風(fēng)格
@property (nonatomic, strong, nullable) UIColor *separatorColor; //設(shè)置分割線顏色
@property (nonatomic, copy, nullable) UIVisualEffect *separatorEffect; // 設(shè)置分割線毛玻璃效果(IOS8之后可用)

@property (nonatomic) BOOL cellLayoutMarginsFollowReadableWidth NS_AVAILABLE_IOS(9_0); //判斷是否需要根據(jù)內(nèi)容留有空白

@property (nonatomic, strong, nullable) UIView *tableHeaderView;    // 設(shè)置tableView頭視圖
@property (nonatomic, strong, nullable) UIView *tableFooterView;      //設(shè)置tableView尾視圖

//從復(fù)用池中取cell
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  
//獲取一個已注冊的cell
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); 
//從復(fù)用池獲取頭視圖或尾視圖
- (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數(shù)據(jù)源--UITableViewDataSource

@required
//返回分組數(shù) 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 返回每組行數(shù)  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional
// 返回每行的單元格 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
// 返回每組頭部標(biāo)題
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    
// 返回每尾部標(biāo)題
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

/**編輯**/
// Cell 在滑動時是否可以編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
// 對 Cell 編輯結(jié)束后的回調(diào)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

/**移動**/
// Cell是否可可以移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
// 對 Cell 移動結(jié)束后的回調(diào)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

/**索引欄**/
// 設(shè)置索引欄內(nèi)容
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;                               
//返回各個索引對應(yīng)的分組
- (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、高度設(shè)置
// 返回每個 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 以后增加的,返回一個粗略值,系統(tǒng)會自動計算)
-(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、附件設(shè)置
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0) __TVOS_PROHIBITED;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
5、選中狀態(tài)設(shè)置
//設(shè)置選中的Cell 是否高亮
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
//Cell高亮?xí)r
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
//Cell取消高亮?xí)r
- (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、編輯
// 設(shè)置tableView被編輯時的狀態(tài)風(fēng)格,如果不設(shè)置,默認(rèn)都是刪除風(fēng)格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
//設(shè)置刪除按鈕的名字
- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;
// 用于自定義創(chuàng)建tableView被編輯時右邊的按鈕,按鈕類型為UITableViewRowAction。
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath; 
// 設(shè)置編輯時是否縮進
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
// 將要編輯時
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath __TVOS_PROHIBITED;
//結(jié)束編輯時
-(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、復(fù)制粘貼
//是否在指定行顯示菜單,返回值為YES時,長按顯示菜單
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
//彈出選擇菜單時會調(diào)用此方法(復(fù)制、粘貼、全選、剪切)
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender NS_AVAILABLE_IOS(5_0);
//選擇菜單項完成之后調(diào)用此方法
- (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);
// 已經(jīng)更新焦點時調(diào)用
-(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、索引欄
// 設(shè)置索引欄內(nèi)容
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H"];
}

//返回各個索引對應(yīng)的分組
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}
3、編輯
//設(shè)置右滑編輯時出現(xiàn)的動作
-(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前交換數(shù)據(jù)
    [self.dataArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
5、復(fù)制、粘貼
//允許 Menu菜單  
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return YES;  
}  

//每個cell都會點擊出現(xiàn)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、設(shè)置行高自動計算
self.tableView.rowHeight = UITableViewAutomaticDimension;
//2、設(shè)置估算行高
self. tableView.estimatedRowHeight = 44;

六、UITableView性能優(yōu)化

關(guān)于 UITableView 的優(yōu)化,網(wǎng)上已經(jīng)有非常多的大神做出了總結(jié),這里沒有什么好說的。推薦來自優(yōu)酷的ibireme大神寫的這篇關(guān)于優(yōu)化的文章。這估計算是比較終極的優(yōu)化方案了。界面流暢度基本可以保持50 ~ 60FPS。《iOS 保持界面流暢的技巧》
另外,如果 Cell 的復(fù)雜度不是特別高,但高度又是不確定的。推薦使用百度團隊開源的 UITableView+FDTemplateLayoutCell ,自動計算及緩存 Cell 的高度,在加快開發(fā)進度的同時,也能確保不錯的流暢度。

1、 確保重用 Cell
2、盡量減少 Cell 的視圖層級,并且少用或不用透明的視圖。避免離屏渲染,比如同時使用

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

3、. 提前計算并緩存 Cell 的高度

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

4、在滑動的時候按需加載
關(guān)于這一點,可以看一下 VVeboTableViewDemo 的實踐。
5、 異步繪制 Cell
6、 盡量不在 cellForRowAtIndexPath 方法中做過多業(yè)務(wù)處理
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);
//刪除時刷新指定的行數(shù)據(jù)。
-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
//添加時刷新指定的行數(shù)據(jù)。
-(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

UITableView 相關(guān)的開源庫

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • UITableView 是 iOS 開發(fā)中必不可少的一個控件,基本上每一個項目都會有多個地方會用到。詳細(xì)的了解UI...
    破夕_____________閱讀 2,225評論 0 4
  • UITableView在iOS開發(fā)中占據(jù)非常重要的位置,必須熟練掌握。學(xué)習(xí)UITableView之前,先了解一下一...
    SmithJackyson閱讀 8,987評論 1 11
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,075評論 4 61
  • 下路送女孩
    帥的人就是我閱讀 314評論 0 1
  • 歲暮神思懶,怕看案牘忙。 因知花令短,持酒醉白旁。 葉落鴉聲冷,風(fēng)搖竹影涼。 空庭梅瓣重,歸去滿襟香。
    江南煙雨閱讀 272評論 4 6

友情鏈接更多精彩內(nèi)容