简体   繁体   English

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

[英]iOS - Add a cell to empty UITableView Section w/ Core Data and NSFetchedResultsController

I'm using Core Data and an NSFetchedResults controller to populate a UITableViewCell (plain style) in my app. 我正在使用Core Data和NSFetchedResults控制器在我的应用程序中填充UITableViewCell(纯样式)。 The TableView has 3 sections, each with a viewForHeaderInSection and a viewForFooterInSection. TableView具有3个部分,每个部分都有一个viewForHeaderInSection和一个viewForFooterInSection。 The user will be entering items that will be put in each section. 用户将输入将放置在每个部分中的项目。

When one of the sections has no items in it, it completely disappears. 当其中一个部分中没有任何项目时,它会完全消失。 I'm wondering if it would be possible to make it so if a section is empty, it would display a new cell that would say "no entries" or something like that (maybe with a UIImageView or another view?), which would then go away if a new item was put into that section. 我想知道是否有可能做到这一点,如果某节为空,它将显示一个新的单元格,上面写着“无条目”或类似的内容(也许带有UIImageView或其他视图?),然后如果在该部分放了新物品,那就走开。

Does anyone know how to accomplish this? 有谁知道如何做到这一点?

Here's the code for my data source. 这是我的数据源的代码。

- (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];
}

Also, the section itself gets removed from the TableView when it has no rows, so how could I make it so the "no entries" cell gets added to the correct section? 另外,该节本身在没有行的情况下也会从TableView中删除,那么我该如何做到这一点,以便“无条目”单元格被添加到正确的节中?

If the section don't appear in the table view, it isn't returning from the fetch results, despite the fact you said you have 3 sections. 如果该部分未出现在表格视图中,则即使您说您有3个部分,也不会从获取结果中返回该部分。 You can return it manually by: 您可以通过以下方式手动将其退回:

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

Then 然后

- (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];
   }
}

In your cellForRowAtIndexPath: test for empty.. 在您的cellForRowAtIndexPath:测试是否为空。

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

} }

Just a idea. 只是一个想法。

EDIT: Be careful, if you try to catch a index that isn't returned by the fetch, your app can crash. 编辑:请小心,如果您尝试捕获未由提取返回的索引,则您的应用程序可能会崩溃。

You need to modify your numberOfRowsInSection function to check if there are no real rows for that section, and return 1 if that is the case (instead of 0). 您需要修改numberOfRowsInSection函数以检查该部分是否没有实际行,如果是这种情况,则返回1(而不是0)。

Then, in the cellForRowAtIndexPath , check if you have data for that section, and just create your "no entries" cell when you see that you don't. 然后,在cellForRowAtIndexPath ,检查是否有该节的数据,并在发现没有该节时创建“无条目”单元。

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

相关问题 iOS - 核心数据 - UITableView - 更新计算值 - NSFetchedResultsController - iOS - core data - UITableView - update calculated value - NSFetchedResultsController 如何将单元格添加到具有核心数据保存信息的UITableView节 - How can i add cell to UITableView Section with core data saved info UITableView中带有NSFetchedResultsController的附加部分 - Additional section in UITableView with NSFetchedResultsController 使用NSPredicate从NSFetchedResultsController和核心数据过滤UITableView - Filtering a UITableView from NSFetchedResultsController and Core Data with NSPredicate 使用UITableView和NSFetchedResultsController确定NSIndexPath - Determine NSIndexPath w/ UITableView & NSFetchedResultsController UITableView部分和按核心数据编制索引 - UITableView section and index by core data 创建一个空部分(标题视图),核心数据中没有任何单元格 - create an empty section(header view) with no cell in core data 将NSFetchedResultsController与UITableView中的特定部分相关联 - Associate NSFetchedResultsController with specific section in UITableView 处理NSFetchedResultsController DidChangeSection IOS核心数据 - Handle NSFetchedResultsController DidChangeSection IOS Core Data iOS - 核心数据 - 一个UIViewController中的多个NSFetchedResultsController - iOS - Core Data - Multiple NSFetchedResultsController in one UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM