使用谓词过滤UITableView的内容

时间:2012-09-01 01:27:47

标签: objective-c ios uitableview core-data

我有一个从CoreData填充的表,我想根据布尔属性进行过滤。如果该地点被标记为收藏,我想在tableview中显示该地点。代码如下:

- (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中添加的谓词,这个当前方法被打破了,建议将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

您应该以这样的方式设置数据数组,使其正确排序以便在表视图中显示。

更好的是,你应该使用NSFetchedResultsController,它的内存效率更高,容易出错。

您不应在cellForRowAtIndexPath中操纵或过滤数据数组。这不太可能有效,而且肯定是非常低效的。

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