gpt4 book ai didi

ios - UITableView:使用 moveRowAtIndexPath:toIndexPath: 和 reloadRowsAtIndexPaths:withRowAnimation: 一起出现损坏

转载 作者:可可西里 更新时间:2023-11-01 05:46:39 25 4
gpt4 key购买 nike

我想使用 iOS 5 漂亮的行移动调用来为 TableView 设置动画以匹配某些模型状态更改,而不是旧式的删除和插入。

更改可能包括重新排序和就地更新,我想为两者设置动画,因此某些行将需要 reloadRowsAtIndexPaths

但是! UITableView 如果更新的单元格因移动而移动位置,则它在存在移动的情况下处理行重新加载似乎完全错误。使用旧的 delete+insert 调用,以一种应该等效的方式,工作正常。

这是一些代码;对于冗长,我深表歉意,但它确实可以编译和运行。关键在于 doMoves: 方法。说明如下。

#define THISWORKS

@implementation ScrambledList // extends UITableViewController
{
NSMutableArray *model;
}

- (void)viewDidLoad
{
[super viewDidLoad];
model = [NSMutableArray arrayWithObjects:
@"zero",
@"one",
@"two",
@"three",
@"four",
nil];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:
#ifdef THISWORKS
@"\U0001F603"
#else
@"\U0001F4A9"
#endif
style:UIBarButtonItemStylePlain
target:self
action:@selector(doMoves:)]];
}

-(IBAction)doMoves:(id)sender
{
int fromrow = 4, torow = 0, changedrow = 2; // 2 = its "before" position, just like the docs say.

// some model changes happen...
[model replaceObjectAtIndex:changedrow
withObject:[[model objectAtIndex:changedrow] stringByAppendingString:@"\u2032"]];
id tmp = [model objectAtIndex:fromrow];
[model removeObjectAtIndex:fromrow];
[model insertObject:tmp atIndex:torow];

// then we tell the table view what they were
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:changedrow inSection:0]]
withRowAnimation:UITableViewRowAnimationRight]; // again, index for the "before" state; the tableview should figure out it really wants row 3 when the time comes
#ifdef THISWORKS
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:fromrow inSection:0]]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:torow inSection:0]]
withRowAnimation:UITableViewRowAnimationAutomatic];
#else // but this doesn't
[self.tableView moveRowAtIndexPath:[NSIndexPath indexPathForRow:fromrow inSection:0]
toIndexPath:[NSIndexPath indexPathForRow:torow inSection:0]];
#endif
[self.tableView endUpdates];
}

#pragma mark - Table view data source boilerplate, not very interesting

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return model.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
[cell.textLabel setText:[[model objectAtIndex:indexPath.row] description]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"this cell was provided for row %d", indexPath.row]];
return cell;
}

代码的作用:建立一个微型模型(小型可变数组);当按下一个按钮时,它会对列表的中间元素进行一个小的更改,并将最后一个元素移到第一个。然后它更新 TableView 以反射(reflect)这些更改:重新加载中间行,删除最后一行并插入一个新行零。

这行得通。事实上,将日志记录添加到 cellForRowAtIndexPath 显示虽然我要求重新加载第 2 行,但 tableview 正确地要求第 3 行,因为在实际执行更新时插入。万岁!

现在注释掉顶部的 #ifdef 以改为使用 moveRowAtIndexPath 调用。

现在 tableview 删除第 2 行,要求新行 2(错误!),并将其插入到最后的第 2 行位置(也是错误的!) .最终结果是第 1 行向下移动了 两个 个槽而不是一个槽,将它滚动到屏幕外以强制重新加载显示它是如何与模型不同步的。如果 moveRowAtIndexPath 以不同的顺序更改了 tableview 的私有(private)模型,我可以理解是否需要在重新加载或模型提取中使用"new"而不是“旧”索引路径,但事实并非如此。请注意,在第二张“之后”图片中,第三行和第四行的顺序相反,无论我重新加载哪个单元格都不会发生这种情况。

before state

after one button push, delete-insert style

after one button push, move-style

我的词汇量越来越多,诅咒苹果。我应该诅咒自己吗?行移动是否与同一更新 block 中的行重新加载完全不兼容(我怀疑还有插入和删除)?在我提交错误报告之前,任何人都可以启发我吗?

最佳答案

我只是花了一些时间研究你的代码,我同意;看起来它只是行不通。

整个领域的文档有点不足,但他们实际上并没有说您可以将 moveRowAtIndexPath:toIndexPath: 与重新加载方法混合使用。它确实说它可以与行插入和行删除方法混合使用。如果我修改您的代码以执行这些代码,这些似乎有效。因此,您可能要求增强功能,而不是提交错误。无论哪种方式,我肯定会将其发送到雷达。

关于ios - UITableView:使用 moveRowAtIndexPath:toIndexPath: 和 reloadRowsAtIndexPaths:withRowAnimation: 一起出现损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11372884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com