UITableView常用屬性和方法基礎

一、屬性

dataSource ---設置UITableViewDataSource的代理

delegate ---設置UItableViewDelegate的代理

 /*當header、footer、cell的高度是固定值的話,用該方法直接設置表的高度,無需調用表的delegate方法設置它們的高度 eg:_table.rowHeight = 60 */
sectionHeaderHeight、sectionFooterHeight、rowHeight -- 設置表頭、尾、cell的高度

sectionIndexColor -- 設置sectionIndexTitle(表索引子母)的顏色

@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;                                                      // show special section index list on right when row count reaches this value. default is 0
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;                   // color used for text of the section index
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;         // the background color of the section index while not being touched
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while it is being touched

估算元素的高度 // NS_AVAILABLE_IOS(7_0)

/*
   連接文章 介紹了estimatedRowHeight的用的場景。(適用動態的cell.height的適配)
*/

理解iOS 8中的Self Sizing Cells和Dynamic Type

estimatedRowHeight   ---  設置表格行的估算高度以改善性能

estimatedSectionHeaderHeight、estimatedSectionFooterHeight   -----  設置Section頭和Section尾估算高度以改善性能

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

separatorEffect  -------- 表的分割線(毛玻璃效果)//默認的分割線的色調暗

效果圖:   

/*
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.jpg"]];
    self.myTableView.backgroundView = backImageView;
    UIBlurEffect *blureffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVibrancyEffect *vinb = [UIVibrancyEffect effectForBlurEffect:blureffect];
    self.myTableView.separatorEffect = vinb;
    //cell.backgroundColor = [UIColor clearColor];
*/

二、方法

初始化方法:
initWithFrame:-----------設置表的大小和位置
initWithFrame:style---------設置表的大小,位置和樣式(組,單一)
setEditing:----------表格進入編輯狀態,無動畫
setEditing: animated:---------表格進入編輯狀態,有動畫
reloadData---------------刷新整個表視圖
reloadSectionIndexTitles--------刷新索引欄
numberOfSections-----------獲取當前所有的組
numberOfRowsInSection:---------獲取某個組有多少行
rectForSection:----------獲取某個組的位置和大小
rectForHeaderInSection:---------獲取某個組的頭標籤的位置和大小
rectForFooterInSection:-----------獲取某個組的尾標籤的位置和大小
rectForRowAtIndex:-----------獲取某一行的位置和大小
indexPathForRowAtPoint-------------點擊某一個點,判斷是在哪一行上的信息。
indexPathForCell:------------獲取單元格的信息
indexPathsForRowsInRect:---------在某個區域裏會返回多個單元格信息
cellForRowAtIndexPath:-------------通過單元格路徑得到單元格
visibleCells-----------返回所有可見的單元格
indexPathsForVisibleRows--------返回所有可見行的路徑

headerViewForSection:--------設置頭標籤的視圖
footerViewForSection;----------設置尾標籤的視圖
UITableViewHeaderFooterView的使用說明

- (nullable UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (nullable UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *HeaderIdentifier = @"header";
    //表頭、尾UITableViewHeaderFooterView的重用機制
    //CustomHeaderView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if(!myHeader) {
    //    [tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderIdentifier];
        myHeader = [[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView"
                                                  owner:self
                                                options:nil] objectAtIndex:0];
    }

    [myHeader.btnSomething setTitle:@"-" forState:UIControlStateNormal];
    [myHeader.lblSomething setText:[NSString stringWithFormat:@"Section: %d",section]];

    return myHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomHeaderView *theHeaderView = (CustomHeaderView*)[tableView <span style="color:#FF0000;">headerViewForSection</span>:indexPath.section];
    NSLog(@"%@",theHeaderView);

    [theHeaderView.lblSomething setAlpha:theHeaderView.lblSomething.alpha-0.1];
    [theHeaderView.btnSomething setTitle:@"+" forState:UIControlStateNormal];
}




重用機制(2個)

dequeueReusableCellWithI dentifier:---------  獲取重用隊列裏的cell單元格
/**< 使用這個方法之前必須是使用了registerNib:forCellReuseIdentifier:<span>或者</span>registerClass:forCellReuseIdentifier:方法註冊了Cell
*/
dequeueReusableHeaderFooterViewWithIdentifier   -------獲取重用隊列裏的UITableViewHeaderFooterView的單元格

註冊一個包含指定標示符的cell or haderFooterView 的nib對象/類
- (void)registerNib:(nullable UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

/*
  beginUpdates and endUpdates兩個方法,是配合起來使用的,標記了一個tableView的動畫塊。分別代表動畫的開始開始和結束。兩者成對出現,可以嵌套使用。
   一般,在添加,刪除,選擇 tableView中使用,並實現動畫效果。在動畫塊內,不建議使用reloadData方法,如果使用,會影響動畫。
*/
調用範例
beginUpdates--------只添加或刪除纔會更新行數
endUpdates---------添加或刪除後會調用添加或刪除方法時纔會更新
insertSections:withRowAnimation:-----------插入一個或多個組,並使用動畫
insertRowsIndexPaths:withRowAnimation:-------插入一個或多個單元格,並使用動畫
deleteSections:withRowAnimation:--------刪除一個或多個組,並使用動畫
deleteRowIndexPaths:withRowAnimation:--------刪除一個或多個單元格,並使用動畫
reloadSections:withRowAnimation:---------更新一個或多個組,並使用動畫
reloadRowIndexPaths:withRowAnimation:-------------更新一個或多個單元格,並使用動畫
moveSection:toSection:-------------移動某個組到目標組位置
moveRowAtIndexPath:toIndexPath:-----------移動個某個單元格到目標單元格位置
indexPathsForSelectedRow----------返回選擇的一個單元格的路徑
indexPathsForSelectedRows---------返回選擇的所有的單元格的路徑
selectRowAtIndexPath:animation:scrollPosition---------設置選中某個區域內的單元格 
deselectRowAtIndexPath:animation:----------取消選中的單元格

UITableViewDataSource代理方法:

numberOfSectionsInTableView:------------設置表格的組數
tableView:numberOfRowInSection:----------設置每個組有多少行
tableView:cellForRowAtIndexPath:---------設置單元格顯示的內容
tableView:titleForHeaderInSection:---------設置組表的頭標籤視圖
tableView:titleForFooterInSection:-----------設置組表的尾標籤視圖
tableView:canEditRowAtIndexPath:---------設置單元格是否可以編輯
tableView:canMoveRowAtIndexPath:--------設置單元格是否可以移動
tableView:sectionIndexTitleForTableView:atIndex:-------設置指定組的表的頭標籤文本
tableView:commitEditingStyle:forRowAtIndexPath:----------編輯單元格(添加,刪除)
tableView:moveRowAtIndexPath:toIndexPath      ------- 單元格移動

tableView:indentationLevelForRowAtIndexPath     -------   返回行層次的深度
效果圖:
             
/*
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [indexPath row];
}
*/

//NS_AVAILABLE_IOS(8_0)
tableView:editActionsForRowAtIndexPath    ----- 自定義左滑動的編輯功能樣式(UITableViewRowAction)
tableView:titleForDeleteConfirmationButtonForRowAtIndexPath   ------ 改變默認左滑動出現的字樣 eg:改變默認默認 Delete 字爲 下載 (return @“下載”;)
 效果圖:
                
/*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{    return UITableViewCellEditingStyleDelete;
}
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *layTopRowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"點擊了刪除");
        [tableView setEditing:NO animated:YES];
    }];
    layTopRowAction1.backgroundColor = [UIColor redColor];
    UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置頂" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"點擊了置頂");
        [tableView setEditing:NO animated:YES];
    }];
    layTopRowAction2.backgroundColor = [UIColor greenColor];
    UITableViewRowAction *layTopRowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"點擊了更多");
        [tableView setEditing:NO animated:YES];
        
    }];
    layTopRowAction3.backgroundColor = [UIColor blueColor];
    NSArray *arr = @[layTopRowAction1,layTopRowAction2,layTopRowAction3];
    return arr;
}
*/


UITableViewDelegate代理方法:

在iOS8以上tableView:willDisplayFooterView:forSection:,和tableView:willDisplayHeaderView:forSection:可以正常被調用

在iOS7(iOS6沒測試)上卻沒有被調用

原來iOS7必須同時實現了Header和Footer這個delegate纔會被調用所以

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.f;
}
 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
tableView:    willDisplayHeaderView: forSection:      --------   設置當前section頭的樣式

tableView:  willDisplayCell:forRowAtIndexPath:-----------設置當前的單元格
效果圖:
            
/*
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.transform = CGAffineTransformMakeTranslation(320, 0);
    [UIView animateWithDuration:indexPath.row*0.2 animations:^{
        cell.transform = CGAffineTransformIdentity;
    }];
}
*/

tableView: heightForRowAtIndexPath:-----------設置每行的高度
tableView:tableViewheightForHeaderInSection:-----------設置組表的頭標籤高度
tableView:tableViewheightForFooterInSection:-------------設置組表的尾標籤高度
tableView: viewForHeaderInSection:----------自定義組表的頭標籤視圖
tableView: viewForFooterInSection: ----------自定義組表的尾標籤視圖
         
tableView:accessoryButtonTappedForRowWithIndexPath:-----------設置某個單元格上的右指向按鈕的響應方法
效果圖:
       
/*
    UIImage *image= [ UIImage imageNamed:@"man" ];
    UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
    CGRect frame = CGRectMake( 0.0 , 0.0 , image.size.width , image.size.height );
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal ];
    button.backgroundColor = [UIColor clearColor ];
    [button addTarget:self
               action:@selector(accessoryButtonIsTapped:event:) forControlEvents:UIControlEventTouchUpInside];

<pre name="code" class="objc">- (void)accessoryButtonIsTapped:(id)sender event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    if(indexPath != nil)
    {
        [self tableView:self.tableView <span style="color:#FF0000;">accessoryButtonTappedForRowWithIndexPath</span>:indexPath];
    }
}

                   
tableView:willSelectRowAtIndexPath:-----------獲取將要選擇的單元格的路徑
tableView:didSelectRowAtIndexPath:-----------獲取選中的單元格的響應事件
tableView: tableViewwillDeselectRowAtIndexPath:------------獲取將要未選中的單元格的路徑
tableView:didDeselectRowAtIndexPath:-----------獲取未選中的單元格響應事件

NS_AVAILABLE_IOS(9_0)       

remembersLastFocusedIndexPath    --------  使用Apple TV遙控器控制屏幕上的用戶界面

- (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);
- (nullable NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView NS_AVAILABLE_IOS(9_0);
cellLayoutMarginsFollowReadableWidth    ------- 判斷是否需要根據內容留有空白(Ipad)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章