本文介绍了为什么程序员使用configureCell:atIndexPath:方法来配置tableView Cell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 好了,直到几天回来,我用来代码 UITableViewCell 中的一切 code> - (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) 编辑:Ok我认为人们对我的错误的想法 我的意思是为什么要创建另一个函数,当你可以将所有的代码放在这个函数 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 我不关心方法名称。解决方案这是因为您可能想更新已经在屏幕上的单元格。而不是完全刷新单元格,你可以简单地从表视图中获取现有单元格,并通过 configureCell:atIndexPath:运行它。如果方法正确实现,这将更新单元格中的所有数据,而没有UITableView删除旧单元格,出列或分配新单元格,并将其放在屏幕上。 对于历史兴趣: 据我所知,我是负责 configureCell:atIndexPath:。我相信其他人已经提出了同样的想法,但我相信,推广它的代码片段最初是由我写的。 NSFetchedResultsControllerDelegate 的早期版本有一个 controllerDidChangeContent:方法,但没有 controllerWillChangeContent:调用,这意味着没有机会调用 - [UITableView beginUpdates ] 我提交了Radar#6708453要求他们添加这个委托方法,并包括一些示例代码向他们展示我想做什么。该代码具有在 refreshCell:atIndexPath:调用中的实际单元格更新逻辑,以便它可以从 tableView:cellForRowAtIndexPath:和控制器:didChangeObject:atIndexPath:forChangeType:newIndexPath:。 out,我发现iOS工程小组不仅添加了我建议的方法,而是将我的错误报告中的示例代码复制到 NSFetchedResultsControllerDelegate文档,虽然他们明智地将名称更改为更容易混淆的 configureCell:atIndexPath: p> 我实际上用Master / Detail iOS Core Data模板开始一个新项目,注意到我的代码和 configureCell:atIndexPath:方法与它 - 在模板中。我跑了一个快速的Google搜索,看看它是否已成为一个共同的约定。看来它有。我对自己的一小块代码感到自豪! Well till a couple of days back I use to code everything for UITableViewCell in- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathmethod. But recently I found out that the iPhone developers (or mac) use configureCell:atIndexPath: and even most of the apples provided source code have this as one of the class function. So my question is basically why do we like to create one more function to provide the cell contents then just write the whole code in cellForRowAtIndexPath: method instead.PS. for people who are not familiar with this then you should see apples source code. and configureCell:atIndexPath: is not another method in UITableViewDatasource, its just a class function that we have in every class that has table view . And we use it like this.- (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];}And more important, after just using this style for trial purpose I got in love this function and now I use it all the time.(When I am using a UITableView)Edit: Ok I think people are getting a wrong idea about my question so let me make it more clear.I meant why to create another function when you can place all your code in this function- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathI am not concern about the method name. 解决方案 It's done because you may want to update cells when they're already on the screen. Rather than completely refreshing the cell, you can simply fetch the existing cell from the table view and run it through configureCell:atIndexPath:. If the method is correctly implemented, this will update all the data in the cell without having UITableView remove the old cell, dequeue or allocate the new cell, and put it on screen.For historical interest:As far as I know, I'm the guy who's responsible for configureCell:atIndexPath:. I'm sure other people have come up with the same idea, but I believe the snippet of code that popularized it was originally written by me. It was then spread by Apple and became a convention.The early versions of NSFetchedResultsControllerDelegate had a controllerDidChangeContent: method, but no controllerWillChangeContent: call, which meant there was no opportunity to call -[UITableView beginUpdates] before changing the contents of the table view.I filed Radar #6708453 asking for them to add this delegate method, and included some example code to show them what I wanted to do. That code had the actual cell updating logic in a refreshCell:atIndexPath: call, so that it could be called from both tableView:cellForRowAtIndexPath: and controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:.When the next beta seed came out, I found that the iOS engineering team not only added the method I suggested, but copied the sample code from my bug report into the NSFetchedResultsControllerDelegate documentation, although they wisely changed the name to the less confusing configureCell:atIndexPath:.I actually started a new project today with the Master/Detail iOS Core Data template, and noticed that my code—and the configureCell:atIndexPath: method with it—was in the template. I ran a quick Google search to see if it had become a common convention. It seems that it has. I'm rather proud of what that little chunk of code has made of itself! 这篇关于为什么程序员使用configureCell:atIndexPath:方法来配置tableView Cell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 13:33