moveRowAtIndexPath cell numbering
问题描述:
我有一个自定义的UITableViewCell叫做CustomCell,它有一个UILabel,它应该显示当前的索引号+ 1(这是一个要做的事情的队列)。moveRowAtIndexPath cell numbering
我正在使用setEditing方法来允许用户移动单元格,但我无法使用以下代码正确编号单元格(按顺序)。基本上我只是试图访问该方法参数传递的区域中的单元格,但数字只是简单地返回无序。我在这里做错了什么?
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
[queuedToDoArray moveObjectAtIndex:fromIndexPath.row toIndex:toIndexPath.row];
NSIndexPath *lowerIndexPath = (fromIndexPath.row < toIndexPath.row ? fromIndexPath : toIndexPath);
NSIndexPath *higherIndexPath = (fromIndexPath.row > toIndexPath.row ? fromIndexPath : toIndexPath);
//Update all the queue numbers in between moved indexes
for (int i = lowerIndexPath.row; i <= higherIndexPath.row; i++) {
NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
CustomCell *currentCell = [todoTable cellForRowAtIndexPath:currentIndexPath];
[currentCell.queueNumber setText:[NSString stringWithFormat:@"%i", currentIndexPath.row + 1]];
}
}
答
你不应该配置a.k.a customCell
你的tableview细胞在moveRowAtIndexPath
方法,相反,只是更新了搬迁行的数据模型数组。
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSString *stringToMove = [self.queuedToDoArray objectAtIndex:sourceIndexPath.row];
[self.queuedToDoArray removeObjectAtIndex:sourceIndexPath.row];
[self.queuedToDoArray insertObject:stringToMove atIndex:destinationIndexPath.row];
}
之后,只需拨打[self.tableView reloadData]
,它会自动为你做。
请查看Apple Developer Document了解更多详情。
编辑:
更好的解决办法是刚刚重装评论相对部分或行作为@ Paulw11。
而不是重新加载整个表,你可以重新加载受影响的行 – Paulw11
@ Paulw11好点,重新加载特定的行是一个更好的方法。 –