为什么程序员使用configureCell:atIndexPath:方法来配置tableView Cell

时间:2011-03-29 04:16:40

标签: iphone cocoa-touch uitableview uikit

直到几天前我才用

中的UITableViewCell编码所有代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法。但最近我发现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

我不关心方法名称。

4 个答案:

答案 0 :(得分:70)

这样做是因为您可能希望在屏幕上已经更新过单元格。您可以简单地从表视图中获取现有单元格,然后通过configureCell:atIndexPath:运行它,而不是完全刷新单元格。如果方法正确实现,这将更新单元格中的所有数据,而无需UITableView删除旧单元格,出列或分配新单元格,并将其放在屏幕上。


对于历史兴趣:

据我所知,我是负责configureCell:atIndexPath:的人。我相信其他人也提出了同样的想法,但我相信推广它的代码片段最初是由我编写的。然后它被Apple传播并成为一个惯例。

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:方法 - 在模板中。我跑了一个快速谷歌搜索,看看它是否已成为一个常见的约定。它似乎有。我为这小块代码本身的成就感到自豪!

答案 1 :(得分:10)

我唯一能看到真正优势,可读性的唯一一次就是你拥有多种细胞类型。然后,您可以使用单独的方法,只知道如何填充特定的细胞类型,如果您有3种不同的细胞类型,对于狗,猫和长颈鹿:

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

那就是说,我自己不使用这种模式。我刚看到其他开发人员在我参与过的项目中使用它。

答案 2 :(得分:8)

可能是因为我们认为自定义单元格的属性和内容属于一个单独的过程,从查找可重用单元格出列,和/或在没有任何内容出列的情况下创建一个新单元格。

答案 3 :(得分: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];

它仍然可以工作:)