你的問題不是你包括代碼示例中重繪你的表。你的問題在別處。我們無法根據此片段診斷問題。您將不得不與我們共享更完整的代碼示例。
與您的問題無關,您的didSelectRowAtIndexPath
中存在一個微妙的問題。你不應該在這裏更新cellAccessoryType
。你真的應該更新你的模型,支持你的用戶界面。如果表中的行數多於在任何給定時刻可見的數量,這將是至關重要的。
爲了說明這個想法,讓我們假設你的模型是一個具有兩個屬性的對象數組,title
的單元格以及單元格是否爲selected
。
因此,您cellForRowAtIndexPath
可能看起來像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
RowData *rowObject = self.objects[indexPath.row];
cell.textLabel.text = rowObject.title;
if (rowObject.isSelected)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
和你didSelectRowAtIndexPath
可能看起來像:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RowData *rowObject = self.objects[indexPath.row];
rowObject.selected = !rowObject.isSelected;
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
同樣,你的編譯器警告/錯誤,無疑是從源的一些其他問題,莖代碼,因爲您的原始代碼段在語法上是正確的。我只是想改正你的didSelectRowAtIndexPath
中的一個不同的缺陷。在MVC編程中,您確實需要確保更新模型(然後更新視圖),而不僅僅是更新視圖。
但是,要清楚的是,如果您不糾正導致當前編譯器警告/錯誤的錯誤,則無論您將哪些內容放入didSelectRowAtIndexPath
中,您可能會收到另一個警告。你必須確定爲什麼編譯器在你當前的代碼中不起作用。
來源
2013-06-04 14:10:10
Rob
indexPath.row代替錯誤行中的indexPath。 – lakesh
你是如何創建'UITableView'的實例的? – Zen
@lakesh這是一個錯誤 – Anupdas