gpt4 book ai didi

iOS - 将单元格添加到带有核心数据和 NSFetchedResultsController 的空 UITableView 部分

转载 作者:行者123 更新时间:2023-11-29 04:32:10 26 4
gpt4 key购买 nike

我正在使用 Core Data 和 NSFetchedResults Controller 来填充我的应用程序中的 UITableViewCell (普通样式)。 TableView 有 3 个部分,每个部分都有一个 viewForHeaderInSection 和一个 viewForFooterInSection。用户将输入将放入每个部分的项目。

当其中一个部分没有任何项目时,它会完全消失。我想知道是否可以做到这一点,如果一个部分是空的,它会显示一个新的单元格,上面写着“没有条目”或类似的内容(也许使用 UIImageView 或另一个 View ?),然后如果将新项目放入该部分,请离开。

有谁知道如何实现这一点吗?

这是我的数据源的代码。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}

此外,当该部分没有行时,该部分本身会从 TableView 中删除,那么我怎样才能将“无条目”单元格添加到正确的部分?

最佳答案

如果该部分没有出现在 TableView 中,则它不会从获取结果中返回,尽管您说您有 3 个部分。您可以通过以下方式手动返回:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

然后

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects] == 0){
return 1;
} else {
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
}

在您的 cellForRowAtIndexPath: 测试中是否为空..

if ([[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects] == 0){
cell.textLabel = @"No Entries";

}

只是一个想法。

编辑:请小心,如果您 try catch 提取未返回的索引,您的应用程序可能会崩溃。

关于iOS - 将单元格添加到带有核心数据和 NSFetchedResultsController 的空 UITableView 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com