如何在重新排序期间更新UITableViewCell

4
当拖动UITableViewCell时,将调用委托方法- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath。在此委托方法中,我可以根据其proposedDestinationIndexPath设置所选单元格的属性。
我的问题是如何在移动过程中更新单元格内容。我可以在释放移动句柄后更新单元格,但我需要根据其位置(proposedDestinationIndexPath)在上下移动时显示更新后的值。
有人有想法如何实现这一点吗?

谢谢@Flex_Addicted。 这是我在Stackoverflow上的第一篇帖子……以后会注意格式。 - Michael Loistl
1个回答

3
您可以在tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:方法中更改单元格的内容。
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:sourceIndexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"Moving Cell to %d %d", proposedDestinationIndexPath.section, proposedDestinationIndexPath.row];
    return proposedDestinationIndexPath;
}

然而,如果您不尝试将单元格移动到新的indexPath,则不会调用此方法。因此,如果您只是将单元格上下移动几个像素,就无法更改文本。但我想您也不应该这样做。


1
谢谢您的快速回复。我想要实现的是一个分级列表,其缩进取决于嵌套级别。因此,移动单元格应更新缩进,并在所选单元格中更新图标(表示该单元格是否为具有子项的节点或仅为没有更多子项的子项)(实时移动)。有一个名为“Outline”的应用程序可以做到这一点...所以肯定是有可能的。 - Michael Loistl
我不需要根据像素位置更新,而是根据 proposedDestinationIndexPath 进行更新。因此,我使用委托方法 'tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:' 在单元格上设置属性。当单元格仍在移动时,它只是不能反映已更新的属性。我尝试过 'setNeedsDisplay',但它也没有更新。 - Michael Loistl
也许你可以展示一下针对targetIndexPathForMoveFromRowAtIndexPath方法的代码。我刚试了Matthias的解决方案,并且能够在移动到不同的proposedDestinationIndexPath时修改单元格内容。 - Andy Friese

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接