我有一個UITableViewController
顯示一堆'n'行(都使用標準UITableViewCell
類),最後一個有一個圖像(一個小'+'圖標來指示'添加記錄')。UITableViewCell仍然顯示圖像重疊文本後清除
假設以下情形:
- 用戶點擊該最後一排,其壓入一個新的視圖控制器到導航堆棧
- 然後用戶填寫表單以添加新的記錄,它使用核心數據
- 形式然後從導航堆棧中彈出,回到你那第一
UITableViewController
,我打電話[self.tableView reloadData]
以確保顯示新的狀態保存
您現在看到'n + 1'行,最後一行顯示'+'圖像。問題是我遇到了一個錯誤,其中'n'行仍然顯示'+'圖像,只是它與該單元格的文本標籤重疊(我可以理解它向其顯示'+'圖像的錯誤文本的左側 - 我明確地設置imageView.image = nil
以避免這種情況 - 但圖像與文本重疊的事實使這一個真的很奇怪)。
下面是我的cellForRowAtIndexPath
函數的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
UITableViewCellStyle cellStyle;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if ((self.selectedCategory != nil) && (indexPath.row < [self.selectedCategory.subjects count])) {
Subject *managedObject = (Subject*)[self.selectedCatSubjects objectAtIndex:indexPath.row];
cell.textLabel.text = managedObject.name;
cell.imageView.image = nil; // Needed in case there used to be a "+" icon
} else {
cell.textLabel.text = @"Add subject...";
[cell.imageView initWithImage:_addIcon];
}
return cell;
}
我認爲這是與細胞的重用。您可以嘗試單獨使用最後一個單元格的不同單元標識符。 – visakh7 2011-04-25 10:31:31
替換NSString * CellIdentifier = @「Cell」;與NSString * CellIdentifier = [NSString stringWithFormat:@「Cell%d%d」,indexPath.section,indexPath.row]; – Krishnabhadra 2011-04-25 10:33:57
@Krishnabhadra我認爲稍微打破了重用細胞的觀點,不是嗎? @ 7KV7的方法聽起來像少了矯枉過正 – andygeers 2011-04-25 10:39:59