limit UITableView size
I'm fetching two kinds of objects in Core Data, some girls and some boys. Each having a size and a born date.
I want to select a maximum of 50 human being, and never more than 10 girls. For instance, if I have 15 girls and 5 boys in my database, I want the tableView to print the 5 boys, and the 10 girls (sorting them with their birthdate).
I differentiate boys from girls with a boolean : sexe.
Do you know what is the best way to do this ?
I can limit the total amount here :
- (NSInteger)tableView:(UITableView开发者_如何学JAVA *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[myFetchedResultsController sections] objectAtIndex:section];
if ([sectionInfo numberOfObjects] > 50) {
return 50;
} else
{
return [sectionInfo numberOfObjects];
}
}
but I'm unable to limit the amount of girls in my UITableView.
Thanks, Niels
Interesting (and I won't comment on the sexism).
Don't see any way to write a predicate to limit one half of a search. Assuming the database doesn't change much during the presentation of the table, one technique might be to pre-select the ten girls first, marking them with a transient property includeGirl, then look for a compound predicate of Boy OR includeGirl with a
[myBoysFetchedResultsController.fetchRequest setFetchLimit:50];
At top of my head i can think of creating a datasource after you read from coredata. Add objects to data source by using this pseudo code logic. It should be pretty simple to replace it with your program variables.
int num_of_girls = 0;
for(i=0;i<50 && data!=nil;i++)
{
// Fetch object from core data at index i
if(data==girl && num_of_girls <15)
{
// Add to data source the data pertaining to girl
num_of_girls++;
i++;
continue
}
// Add to data source as this is pertaining to boy
i++;
}
精彩评论