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

Rob*_*bin 28 iphone cocoa-touch uitableview uikit

好吧,直到几天后,我用它来代码都UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

方法.但最近我发现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];
}
Run Code Online (Sandbox Code Playgroud)

更重要的是,在将这种风格用于试用之后,我爱上了这个功能,现在我一直都在使用它.(当我使用UITableView时)

编辑:好的我认为人们对我的问题有错误的想法,所以让我说清楚一点.

我想为什么要在将此代码放入此函数时创建另一个函数

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

我不关心方法名称.

Bre*_*don 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文档中,尽管他们明智地将名称更改为较少混淆configureCell:atIndexPath:.

我实际上今天用Master/Detail iOS核心数据模板开始了一个新项目,并注意到我的代码 - 以及configureCell:atIndexPath:使用它的方法 - 在模板中.我跑了一个快速的谷歌搜索,看看它是否已成为一个常见的约定.它似乎有.我为自己制作的那小块代码感到自豪!

  • 非常酷和整洁的故事! (6认同)

Nic*_*rge 10

唯一一次,当你有多种细胞类型时,我看到了真正的优势,可读性.那么你可以有单独的方法,只知道如何填充特定的细胞类型,如果你有3种不同的细胞类型,为狗,猫和长颈鹿:

- (void)configureDogCell:(DogCell *)cell atIndexPath:(NSIndexPath *)indexPath
- (void)configureCatCell:(CatCell *)cell atIndexPath:(NSIndexPath *)indexPath
- (void)configureGiraffeCell:(GiraffeCell *)cell atIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

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


Bol*_*ock 8

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


Suw*_*ana 6

[self configureCell:cell atIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

它只是让你的代码干净,易于阅读.

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

你可以称之为任何东西,但大多数人选择这样称呼它.这是因为这个方法在UITableViewCell对象和indexPath对象中传递然后它返回"configed"UITableViewCell,然后返回作为返回对象

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

在otherword中,你可以使用

- (void)configureCellXXX:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

并使用它来调用它

 [self configureCellXXX:cell atIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

它仍然可以工作:)