排序UITableView无法正常工作

Hiv*_*ve7 0 iphone objective-c uitableview nspredicate ios

我是一个排序系统,对我的阵列中的数据进行排序,虽然我在完成的边缘遇到了麻烦.我已经设置了所有系统来做它虽然现在告诉我我有一个错误.以下是有问题的代码段及其给出的错误:

NSArray *filteredArray = [[patients filterUsingPredicate:search] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
Run Code Online (Sandbox Code Playgroud)

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

Bad receiver type void
Run Code Online (Sandbox Code Playgroud)

这是上下文中的代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

是一件事情发生了很多,如果是这样的话有一个方法吗?

提前致谢

Car*_*zey 5

filterUsingPredicate:是一个void方法,因此您无法在其结果上调用方法.此外,您不希望在内部过滤此数组,tableView:cellForRowAtIndexPath:因为这会真正弄乱您的数据.每个细胞都会丢弃一些患者!

尝试:

NSArray *filteredArray = [[patients filteredArrayUsingPredicate:search] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
Run Code Online (Sandbox Code Playgroud)