Objective c 使用NSFetchedResultsController创建分区UITableView
我正在尝试创建一个包含两个不同部分的UITableView。我知道我可以根据托管对象的属性对它们进行分组。例如,如果我想按姓名对它们进行分组,我会: 我返回秒数,如:Objective c 使用NSFetchedResultsController创建分区UITableView,objective-c,xcode,uitableview,nsfetchedresultscontroller,Objective C,Xcode,Uitableview,Nsfetchedresultscontroller,我正在尝试创建一个包含两个不同部分的UITableView。我知道我可以根据托管对象的属性对它们进行分组。例如,如果我想按姓名对它们进行分组,我会: 我返回秒数,如: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[_fetchedResultsController sections] count]; } 但问题是,我不想按名称等属性对其进行分组。我想根据特定的值对它们进行分组,即
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[_fetchedResultsController sections] count];
}
但问题是,我不想按名称等属性对其进行分组。我想根据特定的值对它们进行分组,即一个部分的pud_id=0,另一个部分的pud_id>0
我怎样才能做到这一点?可以使用where子句吗?或者我可以在我的托管对象上创建一个属性,并在sectionNameKeyPath中使用它,例如:
?
谢谢你的意见 也许您可以创建一个NSString类型的瞬态属性,返回两个字符串中的一个
- (NSString *)sectionIdentifier
{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp) {
if (self.pud_id == 0) {
tmp = @"SectionOne";
} else {
tmp = @"SectionTwo";
}
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
return tmp;
}
然后在创建NSFetchedResultsController时使用sectionNameKeyPath:@sectionIdentifier。如果put_id的值发生变化,请确保将sectionIdentifier的原语值设置回nil。根据以前的经验,我发现fetchedresultscontroller只对非常简单的查询非常有用,在这种情况下,您可能需要烘焙自己的fetch,并将两个对象数组排列到另一个ano中,以便在数据源方法中使用。
- (BOOL) hasPudZero {
if (self.pud_id == 0)
return YES;
return NO;
}
- (NSString *)sectionIdentifier
{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp) {
if (self.pud_id == 0) {
tmp = @"SectionOne";
} else {
tmp = @"SectionTwo";
}
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
return tmp;
}