【问题标题】:Removing same object from filtered array and master array从过滤数组和主数组中删除相同的对象
【发布时间】:2018-03-18 18:42:25
【问题描述】:

我有一个可变的对象数组,每个对象都有多个可以在详细视图中显示的属性。

我已经设置了按属性搜索的功能,它工作正常。它根据搜索到的字符显示过滤后的数组,但是当我在该过滤后的数组中滑动删除(尚未从搜索栏中取消)时,它会根据索引路径。

我使用相同的索引路径从主表和相应的对象中删除行(都在commitEditingStyle 内),但正如您可能看到的indexPath.row 过滤的可变数组上的某个对象不会必须在主可变数组上使用相同的indexPath 对应于相同的对象。 (例如,如果主数组是 Bob、Jamie、Sarah、Tom,并且我搜索“To”,Tom 将显示但在索引 0 处,因此当我尝试删除 Tom 时它会删除主数组中的 Bob。)

我希望用户能够从搜索内容中删除项目,并从数组和表中删除相同的项目及其属性。 直接从主表视图和数组正常删除工作正常。代码如下:

- (void)tableView:(UITableView *)tableView commitEditingStyle: 
    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: 
    (NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        if (isFiltered == YES) {
            // Then we are in filtered search results, delete from both arrays and table views

           [self.personArray removeObjectAtIndex:indexPath.row]
           [self.filteredArray removeObjectAtIndex:indexPath.row]
           [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }   else {     // normal table view, just remove at master array and table view

           [self.personArray removeObjectAtIndex:indexPath.row]
           [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
  }
}

您可能会看到indexPath.row 无法匹配每个数组中的相同项目。

【问题讨论】:

  • 使用ObjectClass *person = [self.filteredArray objectAtIndex:indexPath.row]; Find the index of that person in self.personArray` 获取当前人员(名称是唯一的?找出它们的区别,每个属性中的相等性?),并且您有要删除的索引。
  • @Larme 谢谢,这是一个解决方案。作为新成员,我不确定如何使用您的评论将问题标记为已回答。
  • 您不能将评论标记为分辨率。您可以做以下两件事之一:让@Larme 将评论扩展为答案,然后标记它,或者继续post the solution yourself
  • 正如乔希所说,你不能。如果需要,我明天会发布一个完整的解决方案(我离开的地方已经很晚了)。

标签: ios objective-c arrays uitableview


【解决方案1】:

为了清楚起见,我们假设personArrayfilteredArray 填充有MyPerson 类的对象。

假设我们不过滤启动器:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCustomCell *cell = [tableView dequeue...];
    MyPerson *person = [self.personArray objectAtIndex:indexPath.row];
    cell...configureWithThatPerson
    return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
         [self.personArray removeObjectAtIndex:indexPath.row];
         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

你应该在逻辑部分实际上在伪代码中做什么(deletePerson:on:不存在):

MyPerson *personToDelete = [self.personArray objectAtIndex:indexPath.row];
[self deletePerson:personToDelete on:self.personArray];

这当然被简化了(因为你已经知道索引了):

[self.personArray removeObjectAtIndex:indexPath.row];

所以保持这个逻辑:

MyPerson *personToDelete = [self.filteredArray objectAtIndex:indexPath.row];
[self deletePerson:personToDelete on:self.personArray];
[self deletePerson:personToDelete on:self.filteredArray];

现在deletePerson:on:的实现有不同的可能性:
• 在self.personArray 中查找该对象的索引(各种索引,如果它们是重复的?是你的情况)显然对于filteredArray 它是indexPath.row。并将其删除。
• 使用NSPredicate 对其进行过滤。

在所有情况下,您可能需要或不需要实现isEqual:,具体取决于解决方案,但您必须知道两个MyPerson 对象的区别:
它的所有属性都是平等的吗?
您可以检查唯一标识符吗?
只有名字上的匹配?等等。

然后你可以和indexOfObject:indexOfObjectPassingTest:等一起玩。

【讨论】:

    猜你喜欢
    • 2020-06-27
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2021-11-08
    • 2016-08-22
    • 1970-01-01
    相关资源
    最近更新 更多