我想使用 iOS 5 漂亮的行移动调用来为表格视图设置动画以匹配某些模型状态更改,而不是旧式的删除和插入。
更改可能包括重新排序和就地更新,并且我想对两者进行动画处理,因此某些行需要reloadRowsAtIndexPaths
.
But! 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 行,但表视图正确地要求第 3 行,因为一旦真正进行更新时就会插入。哈扎!
现在注释掉顶部的 #ifdef 以使用 moveRowAtIndexPath 调用。
现在是桌面视图removes第 2 行,请求新行2(错误!),并将其插入到最后的 row-2 位置(也是错误的!)。最终结果是第 1 行向下移动two插槽而不是一个,并将其滚动到屏幕外以强制重新加载,显示它是如何与模型不同步的。我可以理解如果moveRowAtIndexPath
以不同的顺序更改了表视图的私有模型,要求在重新加载或模型获取中使用“新”而不是“旧”索引路径,但这不是发生的事情。请注意,在第二个“之后”图片中,第三行和第四行的顺序相反,无论我重新加载哪个单元格,都不会发生这种情况。
我的词汇变得丰富多彩,咒骂苹果。我应该诅咒自己吗?行移动是否与同一更新块中的行重新加载完全不兼容(我怀疑还有插入和删除)?在我提交错误报告之前,有人可以启发我吗?