UITableView重點詳解

jopen 10年前發布 | 13K 次閱讀 iOS開發 移動開發 UITableView

一、UITableView
1.數據展示的條件
1> UITableView的所有數據都是由數據源(dataSource)提供的,所以要想在UITableView展示數據,必須設置UITableView的dataSource數據源對象
2> 要想當UITableView的dataSource對象,必須遵守UITableViewDataSource協議,實現相應的數據源方法
3> 當UITableView想要展示數據的時候,就會給數據源發送消息(調用數據源方法),UITableView會根據方法返回值決定展示怎樣的數據

2.數據展示的過程
1> 先調用數據源的

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    得知一共有多少組

2> 然后調用數據源的

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    得知第section組一共有多少行

3> 然后調用數據源的

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    得知第indexPath.section組 第indexPath.row 行顯示怎樣的cell(顯示什么內容)

3.常見數據源方法
1> 一共有多少組

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

2> 第section組一共有多少行

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

3> 第indexPath.section組 第indexPath.row行顯示怎樣的cell(顯示什么內容)

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

4> 第section組顯示怎樣 的頭部標題

  • (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section;

5> 第section組顯示怎樣的尾部標題

  • (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section;

4.tableView刷新數據的方式
1> 修改模型數據

2> 刷新表格

  • reloadData 整體刷新(每一行都會刷新)
    • (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
      局部刷新

5.性能優化
1> 定義一個循環利用標識
static NSString *ID = @“C1”;

2> 從緩存池中取出可循環利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

3> 如果緩存池中沒有可循環利用的cell

if (cell == nil) {
1   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

}

4> 覆蓋cell上面的數據
cell.textLabel.text = [NSString stringWithFormat:@“第%d行數據”, indexPath.row];

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!