在 UITableView 部分显示核心数据
Posted
技术标签:
【中文标题】在 UITableView 部分显示核心数据【英文标题】:Display Core Data in UITableView Sections 【发布时间】:2014-08-25 11:04:58 【问题描述】:我有一个 NSMutableArray 联系人,我从 CoreData 获得,我想根据数据模型中的属性在带有 Sections 的 UITableView 中显示这些数据。
例如:
具有属性名称(字符串)、部分(整数 16)的数据模型:
-
testuser1, 1
testuser2, 0
testuser3, 0
testuser4, 1
UITableView: (Image-Example)
第 0 节: 单元格 0:testuser2 单元格 1:testuser3
第 1 节: 单元格 0:testuser1 单元格 1:testuser4
我的问题是: 1. 如何计算每个部分的行数? 我知道我必须使用下面的方法,但是我如何计算有多少联系人在部分中有 0 或 1? 2. 我现在如何在右侧显示它们?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// Return the number of rows in the section.
if (section == 0)
return rowsSection0;
else
return rowsSection1;
【问题讨论】:
CoreData 中是否有任何类型可以在部分中拆分用户类型?例如:users:[name:testuser1, type:premium,name:testuser2, type:basic] 是的部分属性:用户:[name: testuser1, type/section: 0, name: testuser2, type/section: 1] iPhone -- breaking up Core Data into sections with NSFetchResultsController的可能重复 【参考方案1】:在 UITableView 中显示 Core Data 记录的最佳方式是使用 NSFetchedResultsController。 您可以找到使用 NSFetchedResultsController here 的教程。
在您的 NSFetchedResultsController init 方法中提供 sectionNameKeyPath 参数值,您要根据该字段的名称创建该部分。在你的情况下“部分”。
也不要忘记相应地设置 NSSortDescriptor。
我还建议您在此处提问之前先 Google 一下。在here 之前也有人问过这个问题。
【讨论】:
我已经在使用 NSFetchedResultsController,也尝试过使用 NSSortDescriptor,但直到现在都没有任何效果。 sectionNameKeyPath 对我来说是新的,会查一下,谢谢!我现在在这个问题上工作了 2 天,已经在谷歌和 *** 上搜索了很多,但没有任何帮助,否则我会问这个问题。 好@AdityaMathur,但这仍然不是答案。您可以将帖子标记并标记为重复,并将您的链接作为评论。提供教程链接但信息很少的答案不被视为答案。 @Neeku 我确实意识到仅提供教程的链接不能被视为答案。但是最初提出问题的方式我认为提供教程链接将是 Mannism 理解 Core Data 和 NSFetchedResultsController 的最佳方式,因为它将让他/她更好地理解。我本可以提供现成的代码,但这对他/她没有帮助。【参考方案2】:NSMutableArray *contacts = []; //your Data array;
NSMutableArray *section0Contacts = [NSMutableArray alloc]init];
NSMutableArray *section1Contacts = [NSMutableArray alloc]init];
for(NSDictionary *dict in contacts)
if([[dict valueForKey:@"type"] isEqualToString:@"section1"])
[section0Contacts addObject:dict];
else if([[dict valueForKey:@"type"] isEqualToString:@"section2"])
[section1Contacts addObject:dict];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if(section ==0)
return [section0Contacts count];
else if(section ==0)
return [section1Contacts count];
else
return 0;
/**
* RETURNS TITLES OF SECTIONS IN UITableView
*/
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
if (section==0) return NSLocalizedString("Section 1", nil);
else if (section==1) return NSLocalizedString("Section 2", nil);
else return @"";
/**
* RETURNS VIEW OF SECTIONS IN UITableView
*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UILabel *sectionTitle = [[UILabel alloc] init];
sectionTitle.frame = CGRectMake(10, 10, 300, 10);
sectionTitle.text = [self tableView:tableView titleForHeaderInSection:section];
sectionTitle.font = [UIFont fontWithName:@"Your font name" size:12];
sectionTitle.textColor = [UIColor blackColor];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350.0f, 30.0f)];
[headerView setBackgroundColor:[constants filterSectionBgColor]];
[headerView addSubview:sectionTitle];
return headerView;
/**
* RETURNS HEIGHT OF SECTIONS IN UITableView
*/
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 30.0f;
【讨论】:
以上是关于在 UITableView 部分显示核心数据的主要内容,如果未能解决你的问题,请参考以下文章
从 XIB 加载的 UITableView Sectionheader 有时显示不正确
使用 fetchedResultsController(核心数据)对部分进行排序以填充我的 UITableView