首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使cellForRowAtIndexPath在移动行后再次被调用?

如何使cellForRowAtIndexPath在移动行后再次被调用?
EN

Stack Overflow用户
提问于 2013-01-20 02:40:15
回答 1查看 1.3K关注 0票数 0

在移动行之后,我更改了与单元格关联的biz的lineandPin编号。如果再次调用cellForRowAtIndexpath,那么一切都会正常进行。

这是我的代码

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSMutableArray *  mutableBusinessBookmarked= self.businessesBookmarked.mutableCopy;
    Business *bizToMove = mutableBusinessBookmarked[sourceIndexPath.row];
    [mutableBusinessBookmarked removeObjectAtIndex:sourceIndexPath.row];
    [mutableBusinessBookmarked insertObject:bizToMove atIndex:destinationIndexPath.row];
    self.businessesBookmarked=mutableBusinessBookmarked;
    [self rearrangePin];
    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
    [self.table reloadData];
}

  1. 我不确定我是不是做对了。我更新了数据模型,但调用moveRowAtIndexPath
  2. [tableView moveRowAtIndexPath...似乎没有做任何事情。无论我是否调用它,行都会被移动。
  3. 我不认为调用self.table reloadData是明智的。但是,我想更新左边的数字。尽管调用了self.table reloadData.

,但仍未调用cellForRowAtindexpath

EN

回答 1

Stack Overflow用户

发布于 2013-01-20 10:22:38

我建议将单元配置逻辑移到一个单独的方法中。然后在moveRowAtIndexPath中,您可以通过直接调用此方法来更新可见单元格。例如:

代码语言:javascript
复制
- (void)configureCell:(UITableViewCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    // Get data for index path and use it to update cell's configuration.
}

- (void)reconfigureVisibleCells
{
    for (UITableViewCell *cell in self.tableView.visibleCells) {
        [self configureCell:cell];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellIdentifier"];
    [self configureCell:cell];
    return cell;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    // Update data model. Don't call moveRowAtIndexPath.
    [self reconfigureVisibleCells];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self configureCell:cell];
}

以下是一些额外的评论:

仅当表格视图需要显示新的单元格时,才会调用

  1. cellForRowAtIndexPath
  2. 当您的数据模型发生更改并且您需要将这种更改传播到UI时,调用moveRowAtIndexpath是合适的。您的情况正好相反,即UI正在将更改传播到您的数据模型。因此,您不会在cellForRowAtIndexPath.

之后调用moveRowAtIndexPath.

  • I always reconfigure cells in willDisplayCell,因为在某些情况下,表视图将覆盖您的自定义设置
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14417524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档