2013-08-18 100 views
0

我是一个排序系统,它对数组中的数据进行排序,尽管我在完成边缘遇到了麻烦。尽管现在告诉我我有一个错误,但我已经设置了所有的系统来完成它。这是有问题的代码段和错误它给:排序UITableView无法正常工作

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; 
} 

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

在此先感谢

回答

5

filterUsingPredicate:是一个无效的方法,所以你不能把它的结果的方法。你也不想在tableView:cellForRowAtIndexPath:里面过滤这个数组,因为这会真的搞乱你的数据。每个细胞你都会丢弃你的一些病人!

尝试:

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

我会怎么做,我是很新的Objective-C。对不起 – Hive7

+0

因为你放了不同的代码,我改变了我的答案。 –

+0

谢谢它的工作完美 – Hive7