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に追加しようとした述語のために壊れており、提案をいただければ幸いです。
ありがとう