【发布时间】:2017-07-16 10:27:12
【问题描述】:
我有一个名为Restaurant 的领域对象。我目前正在支持 iOS 11 基于 UITableView 的拖放重新排序。现在,您将如何存储订单?我为此写了下面的函数。
- (void)tableView:(UITableView *)tableView performDropWithCoordinator:(id<UITableViewDropCoordinator>)coordinator {
NSIndexPath *destinationIndexPath = [[NSIndexPath alloc] init];
if (coordinator.destinationIndexPath != nil) {
destinationIndexPath = coordinator.destinationIndexPath;
} else {
destinationIndexPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)] inSection:([self.tableView numberOfSections]-1)];
}
for (int i = 0; i < [coordinator.items count]; i++) {
id<UITableViewDropItem> item = [coordinator.items objectAtIndex:i];
if (item.dragItem.localObject != nil) {
Restaurant *restaurant = item.dragItem.localObject;
restaurant.order = destinationIndexPath.row;
[restaurant update];
if (self.tableView.hasActiveDrag) {
[self.restaurants removeObject: restaurant];
[self.tableView deleteRowsAtIndexPaths:@[item.sourceIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
[self.restaurants insertObject:restaurant atIndex:destinationIndexPath.row];
[self.tableView insertRowsAtIndexPaths:@[destinationIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
但是,当您对 tableView 重新排序几次时,您可能会为几个 restaurant.order 得到相同的整数。我知道是什么原因造成的,但我不知道如何在不更新所有家餐厅的情况下解决这个问题。有什么想法吗?
【问题讨论】:
标签: ios uitableview realm