繁体   English   中英

如何使用sectionNameKeyPath从NSFetchedResultsController获取numberOfRowsInSection

[英]how to get numberOfRowsInSection from NSFetchedResultsController with sectionNameKeyPath

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

@property (nonatomic, retain) NSNumber * group;

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

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

你实现了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处有一个对象。

迅速:

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
)

暂无
暂无

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

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