// 這樣配置的話超過頁面顯示的內容會重複出現 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定義惟一標識 static NSString *CellIdentifier = @"Cell"; // 經過惟一標識建立cell實例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判斷爲空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用以前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 對cell 進行簡單地數據配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; } //經過如下3方案能夠解決 /*方案一 取消cell的重用機制,經過indexPath來建立cell 將能夠解決重複顯示問題 不過這樣作相對於大數據來講內存就比較吃緊了 */ // 方案一 經過不讓他重用cell 來解決重複顯示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定義惟一標識 static NSString *CellIdentifier = @"Cell"; // 經過indexPath建立cell實例 每個cell都是單獨的 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // 判斷爲空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用以前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 對cell 進行簡單地數據配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; } /* 方案二 讓每一個cell都擁有一個對應的標識 這樣作也會讓cell沒法重用 因此也就不會是重複顯示了 顯示內容比較多時內存佔用也是比較多的和方案一類 */ // 方案二 一樣經過不讓他重用cell 來解決重複顯示 不一樣的是每一個cell對應一個標識 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定義cell標識 每一個cell對應一個本身的標識 NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row]; // 經過不一樣標識建立cell實例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判斷爲空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用以前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 對cell 進行簡單地數據配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; } /* 方案三 只要最後一個顯示的cell內容不爲空,而後把它的子視圖所有刪除,等同於把這個cell單獨出來了 而後跟新數據就能夠解決重複顯示 */ // 方案三 當頁面拉動須要顯示新數據的時候,把最後一個cell進行刪除 就有能夠自定義cell 此方案便可避免重複顯示,又重用了cell相對內存管理來講是最好的方案 前二者相對比較消耗內存 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定義惟一標識 static NSString *CellIdentifier = @"Cell"; // 經過惟一標識建立cell實例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判斷爲空進行初始化 --(當拉動頁面顯示超過主頁面內容的時候就會重用以前的cell,而不會再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } else//當頁面拉動的時候 當cell存在而且最後一個存在 把它進行刪除就出來一個獨特的cell咱們在進行數據配置便可避免 { while ([cell.contentView.subviews lastObject] != nil) { [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview]; } } // 對cell 進行簡單地數據配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; }
事實上最好的方法仍是自定義cell,而後每次從新賦值就行了大數據