UITable View的常用代理方法

//設置行高

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;
    }
    //分區

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    // Return the number of sections.
    return 3;
    }
    //設置每個區有多少行共有多少行

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return 2;
    }

//設置區域的名稱

  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    {
    return @"123";
    }

//是否允許行移動
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{

return YES;

}

//響應點擊事件

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSLog(@"響應單擊事件");
    }

//小按鈕的響應事件

  • (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
    NSLog(@"accessoryButton的響應事件");

}

//刪除按鈕的名字
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除";
}
//設置滑動,
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//ruturn NO不實現滑動
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"手指撮動了");
return UITableViewCellEditingStyleDelete;
// //插入
// return UITableViewCellEditingStyleInsert;
}
設置CELL的樣式

// cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//灰色
// cell.selectionStyle = UITableViewCellSelectionStyleGray;
//無顏色
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
//向右箭頭樣式
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//向右箭頭button
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

UITableViewCellStyleDefault,// Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1,// Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2,// Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle

UITableView 總結

UITableView是UIScrollView的子類,因此它可以自動響應滾動事件(一般為上下滾動)。
它內部包含0到多個UITableViewCell對象,每個table cell展示各自的內容。
當新cell需要被顯示時,就會調用tableView:cellForRowAtIndexPath:方法來獲取或創建一個cell;
而不可視時,它又會被釋放。
由此可見,同一時間其實只需要存在一屏幕的cell對象即可,不需要為每一行創建一個cell。

兩種樣式:group 和 plain

1.協議介紹

UITableViewDataSource(11)

//每個section下cell的個數(必須實現)

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    //通過indexpath返回具體的cell(必須實現)
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

//返回有多少個section(默認是1)

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    //每個section上面的標語內容
  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    //每個section下面的標語內容
  • (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

// Editing
//是否可編輯

  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering
// 是否可拖拽
-tableView:moveRowAtIndexPath:toIndexPath:

  • (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

// Index
//右側索引條需要的數組內容

  • (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
    // return list of section titles to display in section index view (e.g. "ABCD...Z#")

//索引值對應的section-index

  • (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1))

// Data manipulation - insert and delete support
// 對Cell編輯后的回調

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

// 對Cell拖拽后的回調

  • (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
    UITableViewDelegate(常用)

//將要展示Cell/header/Footer視圖回調

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
    //完成展示Cell/header/Footer視圖回調
  • (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath)indexPath NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

// Variable height support
// 每個cell高度的返回(這里高度通過協議返回,是為了table能準確的定位出要顯示的Cell-index,從而滿足UITableView的重用機制)

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    // 每個section-header高度的返回
  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
    // 每個section-footer高度的返回
  • (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// Section header & footer information. Views are preferred over title should you decide to provide both
//可返回每個section-header的自定義視圖

  • (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
    //可返回每個section-footer的自定義視圖
  • (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height

// Selection

// Cell高亮的回調,一般式在選擇的時候才高亮

  • (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

// Cell選中和取消選擇的回調

  • (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  • (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
    // Called after the user changes the selection.
  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
    UITableViewDelegate中的協議還有很多,我只列出了比較常用的,想知道更多的可以查看官方頭文件或官方文檔

2.下拉刷新 上拉加載
MJRefresh

3.重用
UItableView對Cell有一套重用機制,他會將滾出屏幕外的cell放到一個隊列中,滾入屏幕的會從這個隊列中獲取cell,如果沒有再去創建。

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *cellIdentifier = @"cellIdentifier ";



UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:windowReuseIdentifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:windowReuseIdentifier];

    }

return cell;

}

自定義cell

新建cell文件,繼承UITableViewCell

static NSString *cellid = @"cellIdentifier";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if (!cell) {
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
  1. 如果有xib

static NSString *CellIdentifier = @"FriendCell";
FriendCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:@"FriendCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

不使用重用方法

方法1 將獲得cell的方法從- (UITableViewCell)dequeueReusableCellWithIdentifier:(NSString)identifier 換為-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

重用機制調用的就是dequeueReusableCellWithIdentifier這個方法,方法的意思就是“出列可重用的cell”,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell),就可以不使用重用機制,因而問題就可以得到解決,雖然可能會浪費一些空間。

第一個方法如果使用下面插入多次可能會有問題:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
方法2 通過為每個cell指定不同的重用標識符(reuseIdentifier)來解決。

重用機制是根據相同的標識符來重用cell的,標識符不同的cell不能彼此重用。于是我們將每個cell的標識符都設置為不同(@"CMainCell%d", indexPath.row)

5.編輯

//是否可編輯

  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

//返回編輯的類型1.沒有2.刪除3.插入

  • (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

//刪除提示文本

  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

//編輯完成

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
    滑動更多

.盡量不要老調用reloaddata,可能的情況下可以考慮使用

  • (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);

  • (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);
    -(void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容