常規配置如下 當超過tableView顯示的范圍的時候 后面顯示的內容將會和前面重復。
1 // 這樣配置的話超過頁面顯示的內容會重復出現 2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 // 定義唯一標識 5 static NSString *CellIdentifier = @"Cell"; 6 // 通過唯一標識創建cell實例 7 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 8 // 判斷為空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化) 9 if (!cell) { 10 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 11 } 12 // 對cell 進行簡單地數據配置 13 cell.textLabel.text = @"text"; 14 cell.detailTextLabel.text = @"text"; 15 cell.imageView.image = [UIImage imageNamed:@"4.png"]; 16 17 return cell; 18 }
通過以下3方案可以解決
方案一 取消cell的重用機制,通過indexPath來創建cell 將可以解決重復顯示問題 不過這樣做相對於大數據來說內存就比較吃緊了
1 // 方案一 通過不讓他重用cell 來解決重復顯示 2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 // 定義唯一標識 5 static NSString *CellIdentifier = @"Cell"; 6 // 通過indexPath創建cell實例 每一個cell都是單獨的 7 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 8 // 判斷為空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化) 9 if (!cell) { 10 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 11 } 12 // 對cell 進行簡單地數據配置 13 cell.textLabel.text = @"text"; 14 cell.detailTextLabel.text = @"text"; 15 cell.imageView.image = [UIImage imageNamed:@"4.png"]; 16 17 return cell; 18 }
方案二 讓每個cell都擁有一個對應的標識 這樣做也會讓cell無法重用 所以也就不會是重復顯示了 顯示內容比較多時內存占用也是比較多的和方案一類似
1 // 方案二 同樣通過不讓他重用cell 來解決重復顯示 不同的是每個cell對應一個標識 2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 // 定義cell標識 每個cell對應一個自己的標識 5 NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row]; 6 // 通過不同標識創建cell實例 7 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 8 // 判斷為空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化) 9 if (!cell) { 10 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 11 } 12 // 對cell 進行簡單地數據配置 13 cell.textLabel.text = @"text"; 14 cell.detailTextLabel.text = @"text"; 15 cell.imageView.image = [UIImage imageNamed:@"4.png"]; 16 17 return cell; 18 }
方案三 只要最后一個顯示的cell內容不為空,然后把它的子視圖全部刪除,等同於把這個cell單獨出來了 然后跟新數據就可以解決重復顯示
1 // 方案三 當頁面拉動需要顯示新數據的時候,把最后一個cell進行刪除 就有可以自定義cell 此方案即可避免重復顯示,又重用了cell相對內存管理來說是最好的方案 前兩者相對比較消耗內存 2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 // 定義唯一標識 5 static NSString *CellIdentifier = @"Cell"; 6 // 通過唯一標識創建cell實例 7 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 8 9 // 判斷為空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化) 10 if (!cell) { 11 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 12 } 13 else//當頁面拉動的時候 當cell存在並且最后一個存在 把它進行刪除就出來一個獨特的cell我們在進行數據配置即可避免 14 { 15 while ([cell.contentView.subviews lastObject] != nil) { 16 [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview]; 17 } 18 } 19 // 對cell 進行簡單地數據配置 20 cell.textLabel.text = @"text"; 21 cell.detailTextLabel.text = @"text"; 22 cell.imageView.image = [UIImage imageNamed:@"4.png"]; 23 24 return cell; 25 }