iphone - 排序 UITableView 不工作

标签 iphone ios objective-c uitableview nspredicate

我有一个排序系统,可以对数组中的数据进行排序,尽管我在即将完成时遇到了麻烦。我已经设置了所有系统来执行此操作,尽管它现在告诉我有错误。这是有问题的代码片段和它给出的错误:

NSArray *filteredArray = [[patients filterUsingPredicate:search] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

它说的错误是针对患者的,它说:

Bad receiver type void

这是上下文中的代码:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static  NSString *cellIndentifier = @"cell";
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:cellIndentifier forIndexPath:indexPath];
    NSString *letter = [charIndex objectAtIndex:[indexPath section]];
    NSPredicate *search = [NSPredicate predicateWithFormat:@"patientName beginswith[cd] %@", letter];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"patientName" ascending:YES];

    NSArray *filteredArray = [[patients filterUsingPredicate:search] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    if ( nil == cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    NSLog(@"indexPath.row = %d, patients.count = %d", indexPath.row, patients.count);
    Patient *thisPatient = [filteredArray objectAtIndex:[indexPath row]];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", thisPatient.patientName, thisPatient.patientSurname];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = [UIColor blackColor];
    if (self.editing) {
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return cell;
}

这种事情经常发生吗?如果是这样,有没有解决的办法???

提前致谢

最佳答案

filterUsingPredicate: 是一个 void 方法,因此您不能对其结果调用方法。此外,您不希望在 tableView:cellForRowAtIndexPath: 中过滤此数组,因为这确实会弄乱您的数据。您会丢弃一些患者的每个细胞!

尝试:

NSArray *filteredArray = [[patients filteredArrayUsingPredicate:search] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

关于iphone - 排序 UITableView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18296958/

相关文章:

android - Facebook SDK for Unity v7.0.3 FB.Init 委托(delegate)从不调用

ios - 当用户在 Table View 行上切换到 Watchkit 中的另一个详细界面 Controller 时需要将数据传递

objective-c - 执行获取请求 :error: freezes app

iphone - 不同指针类型的比较缺少强制转换

ios - UIViewContainment 子 Controller 大小不正确

iphone - 找不到 -lPods 的库

ios - 崩溃 [上下文 executeFetchRequest :request error:&error] always in Core Data?

ios - 我可以通过代码将 iPad 键盘设置为对接类型/未对接类型/拆分类型吗?

iphone - UIScrollview缩放刷新内容

iphone - #ifdef 检查设备是否是 iPhone 5