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