// 方案一? 通過不讓他重用cell 來解決重復顯示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 定義唯一標識
static NSString *CellIdentifier = @"Cell";
// 通過indexPath創(chuàng)建cell實例 每一個cell都是單獨的
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// 判斷為空進行初始化? --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化)
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"text";
return cell;
}
// 方案二? 同樣通過不讓他重用cell 來解決重復顯示 不同的是每個cell對應一個標識
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 定義cell標識? 每個cell對應一個自己的標識
NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// 判斷為空進行初始化? --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell达址,而不會再次初始化)
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"text";
return cell;
}
// 方案三? 當頁面拉動需要顯示新數(shù)據(jù)的時候嗅义,把最后一個cell進行刪除 就有可以自定義cell 此方案即可避免重復顯示路操,又重用了cell相對內存管理來說是最好的方案 前兩者相對比較消耗內存
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 定義唯一標識
static NSString *CellIdentifier = @"Cell";
// 通過唯一標識創(chuàng)建cell實例
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// 判斷為空進行初始化? --(當拉動頁面顯示超過主頁面內容的時候就會重用之前的cell,而不會再次初始化)
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else//當頁面拉動的時候 當cell存在并且最后一個存在 把它進行刪除就出來一個獨特的cell我們在進行數(shù)據(jù)配置即可避免
{
while ([cell.contentView.subviews lastObject] != nil) {
[(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
}
}
cell.textLabel.text = @"text";
return cell;
}