好,直到幾天後,我使用爲什麼程序員使用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
我不是方法名的關注。
這確實是一個很好的代碼片段。感謝您的貢獻。 – Robin 2012-01-03 10:26:25
非常酷,整潔的故事! – jpswain 2012-05-27 00:45:45
@Robin:你應該接受這個答案。 – BoltClock 2012-08-07 23:35:52