ios – UITableView:使用moveRowAtIndexPath:toIndexPath:和reloadRowsAtIndexPaths:withRowAnimation:一起出现破碎

ios – UITableView:使用moveRowAtIndexPath:toIndexPath:和reloadRowsAtIndexPaths:withRowAnimation:一起出现破碎,第1张

概述我想使用iOS 5的漂亮的行移动调用来动画一个tableview以匹配一些模型状态更改,而不是旧式的delete-and-insert. 更改可能包括重新排序和就地更新,我想为两者设置动画,因此某些行将需要reloadRowsAtIndexPaths. 但!如果更新的单元格由于移动而移位,则UITableView在移动存在的情况下处理行重新加载时似乎完全错误.使用旧的删除插入调用,以一种应该是等效 我想使用iOS 5的漂亮的行移动调用来动画一个tablevIEw以匹配一些模型状态更改,而不是旧式的delete-and-insert.

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

但!如果更新的单元格由于移动而移位,则UItableVIEw在移动存在的情况下处理行重新加载时似乎完全错误.使用旧的删除插入调用,以一种应该是等效的方式,工作正常.

这是一些代码;我为冗长而道歉,但它确实编译并运行.肉是在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;}

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

这有效.实际上,向cellForRowAtIndexPath添加日志记录表明虽然我要求重新加载第2行,但是tablevIEw正确地要求第3行,因为一旦实际进行更新,就会插入第3行.好哇!

现在注释掉顶部的#ifdef以使用moveRowAtIndexPath调用.

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

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

解决方法 我只是花了一些时间玩你的代码,我同意;看起来它只是不起作用.

整个区域有点记录不足,但它们实际上并没有说你可以将moveRowAtIndexPath:toIndexPath:与reload方法混合使用.它确实说它可以与行插入和行删除方法混合使用.如果我修改你的代码来代替它们,那些似乎是有效的.所以,你可能会要求增强,而不是提交错误.无论哪种方式,我肯定会把它发送到雷达.

总结

以上是内存溢出为你收集整理的ios – UITableView:使用moveRowAtIndexPath:toIndexPath:和reloadRowsAtIndexPaths:withRowAnimation:一起出现破碎全部内容,希望文章能够帮你解决ios – UITableView:使用moveRowAtIndexPath:toIndexPath:和reloadRowsAtIndexPaths:withRowAnimation:一起出现破碎所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1038275.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存