UITableView 01

UITableView

tableView 要顯示數據的時候, 要依賴數據源代理

必須實現三個方法
1. tableView 有多少組
numberOfSectionsInTableView:

2. 每一組有多少行
numberOfRowsInSection:

3. 每一行要顯示的內容
cellForRowAtIndexPath:

// 設置組頭和組尾 文本
titleForHeaderInSection:

titleForFooterInSection:


// 設置 組頭view的高度
_tableView.sectionHeaderHeight = 100;

// 設置 組尾view的高度
_tableView.sectionFooterHeight = 50;

 1.選中某一個cell的時候, 就會調用這個方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
}

2. 取消選中會自動調用這個方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
   
   
 
}


tableViewCell 的重用機制
tableView
自身維護了一個緩存池

1. 定義重用標識符
2. 根據重用標識符到緩存中去找對應的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

3. 對取到的cell 進行判斷, 如果找不到就重新實例化cell
實例化的時候, 一定要設置重用標識符 :  identifier
if (nil == cell ) {
    cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}


注意:
1. 一定要先修改數據源中的數據
2. 進行刷新


1. 刷新全部數據
[_tableView reloadData];

2. 刷新指定行 數據


NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:
0];

[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];





// 是否隱藏 tableView 的狀態欄
- (
BOOL)prefersStatusBarHidden {
   
return YES;
}

發佈了37 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章