简体   繁体   English

限制UITableView的大小

[英]limit UITableView size

I'm fetching two kinds of objects in Core Data, some girls and some boys. 我正在Core Data中获取两种对象,一些女孩和一些男孩。 Each having a size and a born date. 每个都有大小和出生日期。

I want to select a maximum of 50 human being, and never more than 10 girls. 我想最多选择50个人,而女孩最多不能超过10个。 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). 例如,如果我的数据库中有15个女孩和5个男孩,则希望tableView打印5个男孩和10个女孩(将它们与出生日期排序)。

I differentiate boys from girls with a boolean : sexe. 我用布尔值将性别与女孩区分开:sexe。

Do you know what is the best way to do this ? 你知道什么是最好的方法吗?

I can limit the total amount here : 我可以在这里限制总数:

- (NSInteger)tableView:(UITableView *)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. 但我无法限制UITableView中的女孩数量。

Thanks, Niels 谢谢,尼尔斯

At top of my head i can think of creating a datasource after you read from coredata. 从我的头顶上,我可以想到在读取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++;
}

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 假设数据库在表的显示过程中变化不大,一种技术可能是首先预先选择十个女孩,将它们标记为瞬态属性includeGirl,然后寻找Boy或includeGirl的复合谓词。

[myBoysFetchedResultsController.fetchRequest setFetchLimit:50];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM