我有一个从 CoreData 填充的表,我想根据布尔属性进行过滤。如果该地点被标记为收藏,我想在表格视图中显示该地点。代码如下:
- (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 中添加谓词,此当前方法已损坏,我们将不胜感激。
谢谢