2013-08-18 96 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