1. 程式人生 > >UITableView複用,禁止複用總結

UITableView複用,禁止複用總結

可以參考

 

建立方式彙總,註冊和不註冊

Cell註冊的兩種方式
1.tableView registerNib:(nullable UINib *) forCellReuseIdentifier:(nonnull NSString *)
2.tableView registerClass:(nullable Class) forCellReuseIdentifier:(nonnull NSString *)

Cell註冊的形式:

(1)系統cell
    1.註冊
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    2.不註冊
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

(2)自定義cell
    1.註冊
    [self.tableView registerClass:[xxxxCell class] forCellReuseIdentifier:@"cell"];
    xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

  2.不註冊
    xxxxCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
  if (cell==nil) {
      cell=[[xxxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

(3)自定義cellXib註冊
    1.註冊(可以複用)
    [tableView registerNib:[UINib nibWithNibName:@"xxxxViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
    xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    2.不註冊(不會複用,每一次都是重新建立)
     xxxxCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@“xxxxCell" owner:self options:nil]lastObject];
    }

複用機制不多做贅述,只講解一下注冊的複用機制

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier: identifier] ;

這個方法的作用是,當我們從重用佇列中取cell的時候,如果沒有,系統會幫我們建立我們給定型別的cell,如果有,則直接重用. 這種方式cell的樣式為系統預設樣式.在設定cell的方法中只需要:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 重用佇列中取單元格 由於上面已經註冊過單元格,系統會幫我們做判斷,不用再次手動判斷單元格是否存在

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier forIndexPath:indexPath] ;

    return cell ;
}

現在記錄一種禁用複用的場景,淘寶詳情頁面相關的場景,在不同的cell中建立不同的檢視,而且不實用複用機制,讓每一個cell儲存自己當前的頁面.
只講通過xib建立的檢視
使用方法,採用註冊xib做法
註冊四種cell

[tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailTopViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailTopViewCellID];
    [tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailMidViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailMidViewCellID];
    [tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailBottomViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailBottomViewCellID];
    [tableView registerNib:[UINib nibWithNibName:@"BXFHomeDetailBottomTableViewCell" bundle:nil] forCellReuseIdentifier:kHomeDetailBottomTableViewCellID];
if (section==0) {
        TopViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kHomeDetailTopViewCellID];
        NSLog(@"%@---",cell.textFieldstr.text);
        return cell;
    }else if(section==1){
        MidViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kHomeDetailTopViewCellID];
        NSLog(@"%@---",cell.textFieldstr.text);
        return cell;
    }else if(section==2){
        BottomViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kHomeDetailTopViewCellID];
        NSLog(@"%@---",cell.textFieldstr.text);
        return cell;
    }

建立cell的時候分別建立,因為是通過註冊建立,當沒有cell的時候系統自動幫你建立,並且放到複用池當中,所以你對每行的cell做一次操作之後,cell會記錄所有的操作.當滑出螢幕的時候,系統放到複用池當中(注意,cell已經完全保留相關的操作),當再次出現到螢幕上時,會從複用池中獲取對應的cell,其中的相關資訊儲存完全,不用重新載入.
這樣就保證了每一個cell上的檢視儲存了相關操作的內容,當時在檢視內巢狀的tableView重新整理之類的需要自己重新手動重新整理一次,否則不會重新賦值會導致不能展示