【问题标题】:how to get numberOfRowsInSection from NSFetchedResultsController with sectionNameKeyPath如何使用 sectionNameKeyPath 从 NSFetchedResultsController 获取 numberOfRowsInSection
【发布时间】:2013-10-21 00:31:12
【问题描述】:

我有一个具有“组”属性的实体,因为我想按“组”(0 或 1)将我的实体列表分为 2 个部分

@property (nonatomic, retain) NSNumber * group;

在我的 fetchedResultsController 中,我将 sectionNameKeyPath 指定为“组”

self.fetchedResultsController = [[NSFetchedResultsController alloc] 
    initWithFetchRequest:request managedObjectContext:self.managedObjectContext 
    sectionNameKeyPath:@"group" cacheName:nil];

如何在下面的方法中返回每个部分的行数?我收到以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: 
'-[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array'

代码如下:

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

我也试过了,同样的错误:

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]
    objectAtIndex:section];
return [sectionInfo numberOfObjects];

注意我也实现了这个方法:

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

【问题讨论】:

    标签: ios uitableview core-data nsfetchedresultscontroller


    【解决方案1】:

    你实现numberOfSectionsInTableView:了吗?如果您不实现它,tableView 假定您有 1 个部分,如果 fetchedResultsController 没有任何部分(即它没有对象),这将导致此异常。

    您必须在numberOfSectionsInTableView: 中返回[sections count]

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

    如果你总是想显示两个部分,你必须检查 fetchedResultsController 是否有请求的部分。如果 fetchedResultsController 没有此部分,请不要向它询问此操作中的对象数。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 2;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        NSInteger count = 0;
        NSInteger realNumberOfSections = [self.fetchedResultsController.sections count];
        if (section < realNumberOfSections) {
            // fetchedResultsController has this section
            id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
            count = [sectionInfo numberOfObjects];
        }
        else {
            // section not present in fetchedResultsController
            count = 0; // for empty section, or 1 if you want to show a "no objects" cell. 
        }
        return count;
    }
    

    如果你在 else 中返回 0 以外的值,你也必须更改 tableView:cellForRowAtIndexPath:。与此方法类似,您必须检查 fetchedResultsController 在请求的 indexPath 处是否有对象。

    【讨论】:

    • 我实现了那个方法,但我特别返回了 2,因为我想在场景的第一个视图上显示 2 个部分,即使还没有记录 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; }
    • 你不能这样。我会为此添加一个编辑。
    • 如果我还没有要分组的实体的任何记录,我应该如何强制在开头显示 2 个部分?
    • 用同样的方法?如果您在 tableView:numberOfRowsInSection: 中返回“错误”值,则必须更改几种方法。例如tableView:titleForHeaderInSection: 和所有其他向 fetchedResultsController 询问对象或部分的人。
    • 你说我需要把它放在 numberOfSections 方法中...返回 [self.fetchedResultsController.sections count];...但是它根本没有显示任何部分...原因为什么我将它硬编码为 2。我还硬编码了返回的值,例如,titleForHeaderInSection 因为我预先知道我有 2 个部分......但是,只有当我评论这 2 行并为两者都返回 1 时(因为我需要为每个添加部分显示 1 个单元格),然后我没有错误::: id ... count = [sectionInfo numberOfObjects]...
    【解决方案2】:

    斯威夫特:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       let section = fetchedResults.sections?[section]
       let sectionCnt = section?.numberOfObjects
    
       return sectionCnt!
    }
    

    首先获取该部分。计算其中的对象数。

    注意:为此,fetchedResults 对象必须知道如何将数据划分为多个部分!这是在获取请求时实现的。在这一行中,将用于划分部分的 CoreData 属性名称传递给它:

    fetchedResults = NSFetchedResultsController(
        fetchRequest: fetchRequest, 
        managedObjectContext: managedContext,
        sectionNameKeyPath: "CD_Attribute_Name_GoesHere",
        cacheName:nil
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2012-05-13
      • 2014-07-30
      • 1970-01-01
      • 2011-09-30
      相关资源
      最近更新 更多