【问题标题】:Store ordering in Realm在 Realm 中进行商店订购
【发布时间】: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


    【解决方案1】:

    领域已经有一个有序的属性类型:List

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您可以将名为 restaurant_order 的列初始设置为 0。成功拖放后,您必须将所有项目订单设置到领域中,并在获取结果时使用 sortedResultsUsingProperty:@"restaurant_order" 代码并设置所有餐厅列表。

        希望对您有所帮助...

        【讨论】:

          猜你喜欢
          • 2023-04-07
          • 2023-03-06
          • 1970-01-01
          • 1970-01-01
          • 2014-03-26
          • 2011-12-04
          • 1970-01-01
          • 2017-09-07
          • 1970-01-01
          相关资源
          最近更新 更多