2012-09-01 100 views
0

我有一个从CoreData填充的表,我想根据布尔属性进行过滤。如果地点被标记为最爱,我想在桌面视图中显示地点。代码如下:使用谓词过滤UITableView的内容

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.clubs count]; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    self.type = del.type; 
    self.searchTxt = del.searchTxt; 
    //Core data fetch 
    NSManagedObjectContext *managedObjectContext = [[SDCoreDataController sharedInstance] backgroundManagedObjectContext]; 
    if(!self.clubs){ 
     self.clubs = [[NSArray alloc] init]; 
    } 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Place"]; 
    fetchRequest.sortDescriptors =[NSArray arrayWithObjects:sortDescriptor, nil]; 
    [sortDescriptor release]; 

    [managedObjectContext performBlockAndWait:^{ 
     NSError *error = nil; 
     self.clubs = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    }]; 

    if (self.clubs == nil) { 
     // Handle the error 
    } 

} 




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    NSPredicate *predicate; 
    predicate = [NSPredicate predicateWithFormat:@"favourite == 0"]; 
    NSArray *myArray2 = [self.clubs filteredArrayUsingPredicate:predicate]; 
    Place * place = [myArray2 objectAtIndex:indexPath.row]; 

    PlaceListCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[PlaceListCustomCell alloc] initWithFrame:CGRectZero ]autorelease]; 
    } 



    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"table_row_bg.png"]]; 

    cell.backgroundColor = background; 

    [background release]; 
    // Configure the cell 
    cell.placeName.text = place.name; 
    cell.townName.text = place.addressline2; 
    cell.type.text = place.type; 
    cell.disclosure.image = [UIImage imageNamed:@"disclosure_icon.png"]; 
    if(place.promo ==[NSNumber numberWithBool:YES]){ 
     cell.promotag.image = [UIImage imageNamed:@"promo_tag.png"]; 
    } 
    cell.reviewNumber.text = @"0 Reviews"; 
    cell.reviewView.image = [UIImage imageNamed:@"rating_3.png"]; 
    //Check if there is an image. If not, set the default image 
    if (place.image != nil) 
    { 
     cell.imageView.image = [UIImage imageWithData:place.image]; 

    } 
    else 
    { 
     cell.imageView.image = [UIImage imageNamed:@"no-image-available.jpg"];  
    } 

    return cell; 

} 
@end 

该电流方法由于我试图在cellForRow添加和建议将不胜感激谓词断裂。

感谢

回答

0

你必须让你的数据阵列中的这样一种方式,它已经被正确的排序为表视图的显示。

更好的是,你应该使用一个NSFetchedResultsController,这是更高效的内存和更少的错误。

您不应该在cellForRowAtIndexPath中操纵或过滤数据数组。这是不太可能的,肯定会大量低效。

而且请不要将不相关的代码转储到问题中。