我似乎無法獲得dequeueReusableCellWithIdentifier
的工作。dequeueReusableCellWithIdentifier不適用於ARC和IOS 4
我需要建立一個iOS 4的項目,所以我不能使用故事板,但我使用ARC。
假設我有2個部分,每個部分都有1行。
看下面的代碼,我使用strong屬性來傳遞所有權,因爲ARC會插入「autorelease」代碼。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"TableCellIdentifier";
MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
self.retainedCell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
但是,每次函數被調用時,對於每一行,單元總是零(因此一個新的MainTableCell被分配)。細胞永遠不會被重複使用。
這不會是一個問題,除非我以編程方式調用tableView:cellForRowAtIndexPath:,這意味着我每次都得到一個新分配的單元格,而不是現有的單元格。
我能看到的唯一方法是將單元格添加到NSMutableArray。
有什麼我現在不知道與dequeueReusableCellWithIdentifier
?
謝謝!
編輯 我正在使用下面的代碼來獲取單元格。如前所述,它正在創建一個新的單元格,而不是重新使用應該已經制作或保留的單元格。 我不需要爲所有行調用reloadData,只需更改一個特定的行即可。
MainTableCell *cell = (MainTableCell *)[self tableView:self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[self configureCell:cell atIndexPath:indexPath];
你只有一個行中的每個部分,然後將有兩行可見,我不認爲你行出去的觀點和你的細胞重複使用..請附上屏幕截圖你表視圖 –
你自己調用這個方法嗎?你爲什麼這樣做?你能否解釋一點,因爲它幾乎肯定是錯誤的。 – jrturton
這完全沒有錯。我需要對特定的UITableViewCell做一些具體的事情,因此我需要對它進行引用。 – mmilo