Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 域中的存储排序_Ios_Uitableview_Realm - Fatal编程技术网

Ios 域中的存储排序

Ios 域中的存储排序,ios,uitableview,realm,Ios,Uitableview,Realm,我有一个叫做餐馆的领域对象。我目前正致力于支持iOS 11基于拖放的UITableView重新排序。现在,您将如何存储订单?我写了下面的函数来解决这个问题 - (void)tableView:(UITableView *)tableView performDropWithCoordinator:(id<UITableViewDropCoordinator>)coordinator { NSIndexPath *destinationIndexPath = [[NSIndexP

我有一个叫做餐馆的领域对象。我目前正致力于支持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的相同整数。我知道是什么导致了这一点,但我不知道如何在不每次更新所有餐厅的情况下解决这一问题。有什么想法吗?

您可以选择一个名为restaurant\u order的列,初始设置为0。成功拖放后,您必须将所有项目顺序设置到域中,并在获得结果时使用sortedResultsUsingProperty:@restaurant\u order code并设置所有餐厅列表


希望对您有所帮助…

您可以选择一个名为restaurant\u order的列,初始设置为0。成功拖放后,您必须将所有项目顺序设置到域中,并在获得结果时使用sortedResultsUsingProperty:@restaurant\u order code并设置所有餐厅列表


希望对您有所帮助…

您不需要每次都更新所有餐厅-您只需重新编号目的地和来源地之间的餐厅

您不需要每次更新所有餐厅-您只需重新编号目的地和来源地之间的餐厅

域已具有有序属性类型:


使用此类型将保留排序,而无需在您的模型上存储索引顺序。

Realm已具有排序属性类型:

使用此类型将保留排序,而无需在模型上存储索引顺序