2011-03-29 29 views
26

好,直到幾天後,我使用爲什麼程序員使用configureCell:atIndexPath:方法來Config中的tableView細胞

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

方法家居UITableViewCell編碼。但最近我發現iPhone開發者(或Mac)使用configureCell:atIndexPath:,甚至大部分蘋果提供的源代碼都將其作爲類功能之一。 所以我的問題基本上是爲什麼我們想創建一個更多的功能來提供單元格內容,然後只需在cellForRowAtIndexPath:方法中寫入整個代碼。

PS。對於那些不熟悉這個的人來說,你應該看到蘋果的源代碼。而configureCell:atIndexPath:不是UITableViewDatasource中的另一種方法,它只是我們在每個具有表視圖的類中都有的類函數。我們這樣使用它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.titleLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; 
} 

而更重要的,只是用這種風格審判的目的後,我就愛上了這個功能,現在我用它所有的時間(當我使用一個UITableView)

編輯:好的,我認爲人們對我的問題有錯誤的想法,所以讓我更清楚一點。

我的意思是,爲什麼要創建另一個功能時,您可以將所有的代碼在此功能

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

我不是方法名的關注。

回答

66

這樣做是因爲您可能想要在單元格已經在屏幕上時更新單元格。與其完全刷新單元格,您可以簡單地從表格視圖中獲取現有單元格並通過configureCell:atIndexPath:運行它。如果方法被正確實現,這將更新單元格中的所有數據,而不必讓UITableView移除舊單元格,出列或分配新單元格並將其放到屏幕上。


對於歷史的興趣:

據我所知,我是誰負責configureCell:atIndexPath:的傢伙。我相信其他人也提出了相同的想法,但我相信推廣它的代碼片段最初是由我編寫的。然後它被蘋果公司傳播併成爲一項公約。

NSFetchedResultsControllerDelegate的早期版本有一個controllerDidChangeContent:方法,但沒有controllerWillChangeContent:調用,這意味着在更改表視圖的內容之前沒有機會調用-[UITableView beginUpdates]

我提交了Radar#6708453要求他們添加這個委託方法,並且包含一些示例代碼來向他們展示我想要做的事情。該代碼在refreshCell:atIndexPath:調用中具有實際的單元更新邏輯,因此可以從tableView:cellForRowAtIndexPath:controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:中調用它。

當下一個測試版種子出來時,我發現iOS工程團隊不僅添加了我建議的方法,而且將示例代碼從我的錯誤報告中複製到NSFetchedResultsControllerDelegate documentation,儘管他們明智地將名稱更改爲較少混淆configureCell:atIndexPath:

我實際上是用Master/Detail iOS核心數據模板啓動了一個新項目,並注意到我的代碼和它的configureCell:atIndexPath:方法在模板中。我跑了一個快速谷歌搜索,看看它是否已經成爲一個共同的慣例。它似乎已經。我很自豪這個小塊代碼是由它自己做的!

+0

這確實是一個很好的代碼片段。感謝您的貢獻。 – Robin 2012-01-03 10:26:25

+5

非常酷,整潔的故事! – jpswain 2012-05-27 00:45:45

+0

@Robin:你應該接受這個答案。 – BoltClock 2012-08-07 23:35:52

8

可能是因爲我們認爲自定義單元格的屬性和內容屬於單獨的過程,無法查找可重用的單元以便出列和/或在沒有任何出列條件時創建新單元。

6
[self configureCell:cell atIndexPath:indexPath]; 

它只是在那裏讓你的代碼乾淨,易於閱讀。

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 

你可以稱之爲任何東西,但大多數人選擇這樣稱呼它。這是因爲這種方法得到了傳遞的UITableViewCell對象,indexPath對象然後返回「的ConfigEd」的UITableViewCell,然後獲取返回的回報對象

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
在otherword

,您可以使用

- (void)configureCellXXX:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 

,並使用

[self configureCellXXX:cell atIndexPath:indexPath]; 

調用它,它仍然會工作:)

+1

函數的好名字'XXX' .. – Robin 2011-03-29 04:53:38

10

唯一一次,我看到了真正的優勢,可讀性明智,是當你有多個單元格類型。然後,你可以有不同的方法,只有知道如何來填充特定的細胞類型,比如,如果你有3種不同類型的細胞,狗,貓和長頸鹿:

- (void)configureDogCell:(DogCell *)cell atIndexPath:(NSIndexPath *)indexPath 
- (void)configureCatCell:(CatCell *)cell atIndexPath:(NSIndexPath *)indexPath 
- (void)configureGiraffeCell:(GiraffeCell *)cell atIndexPath:(NSIndexPath *)indexPath 

這麼說,我不使用這個模式我。我剛剛看到它在其他開發人員使用過的項目中使用過。

相關問題