2012-05-18 72 views
2

我似乎無法獲得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]; 
+0

你只有一個行中的每個部分,然後將有兩行可見,我不認爲你行出去的觀點和你的細胞重複使用..請附上屏幕截圖你表視圖 –

+0

你自己調用這個方法嗎?你爲什麼這樣做?你能否解釋一點,因爲它幾乎肯定是錯誤的。 – jrturton

+0

這完全沒有錯。我需要對特定的UITableViewCell做一些具體的事情,因此我需要對它進行引用。 – mmilo

回答

3

碰巧你MainTableCell出現排隊去,然後你繼續檢查,如果它是零,此時您使用完全不同的變種來的Alloc表格單元格。有沒有搞錯?試試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"TableCellIdentifier"; 
    MainTableCell *cell = (MainTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[MainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 
+0

如果有人可以正確格式化,我會非常感激(膽汁流動移動SO)。 – CodaFi

+1

我覺得你的痛苦...我的答案中有90%是通過手機完成的。鏈接也很有趣。 – jrturton

+0

不幸的是,正如我提到的屬性是嘗試保留該變量。你上面的代碼是我嘗試的第一件事(它非常基本),但沒有奏效。 – mmilo

相關問題