如何结合CoreData给UITableView的section填充数据
2013-03-28 17:23
691 查看
搬运自StackOverFlow:numberOfRowsInSection: method for core data and multiple sections‘
问题描述:一个tableview中建立两个section,第一个section使用NSArray填充数据,第二个section使用coredata填充数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
} else {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
}
最开始的代码返回错误
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
解决方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
} else {
NSUInteger frcSection = section - 1;
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:frcSection];
return [sectionInfo numberOfObjects];
}
}
原因解释:
coredata中的fetched results controller只有一个section(section #0),但我们却想在第二个section(section
#1)里面显示FRC所取得的数据
在cellforrow这个方法里也要对section的数字减一
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
cell.textLabel.text = entityOne.name; //entityOne object passed from previous VC
} else {
NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 1)];
entityTwo = [self.fetchedResultsController objectAtIndexPath:frcIndexPath];
cell.textLabel.text = entityTwo.name;
}
}
问题描述:一个tableview中建立两个section,第一个section使用NSArray填充数据,第二个section使用coredata填充数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
} else {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
}
最开始的代码返回错误
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
解决方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
} else {
NSUInteger frcSection = section - 1;
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:frcSection];
return [sectionInfo numberOfObjects];
}
}
原因解释:
coredata中的fetched results controller只有一个section(section #0),但我们却想在第二个section(section
#1)里面显示FRC所取得的数据
在cellforrow这个方法里也要对section的数字减一
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
cell.textLabel.text = entityOne.name; //entityOne object passed from previous VC
} else {
NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 1)];
entityTwo = [self.fetchedResultsController objectAtIndexPath:frcIndexPath];
cell.textLabel.text = entityTwo.name;
}
}
相关文章推荐
- UITableView,UICollectionView和CoreData完美结合进行增删改操作,如何导入大量数据,如何利用NSEntityMigrationPol
- NSFetchedResultsController和UITableView显示CoreData的数据时用relationship分组的方 ...
- ios Coredata 关联 UITableView 数据自动更新
- ios Coredata 关联 UITableView 数据自动更新
- UITableView——reloadData与reloadSection性能比较
- UITableView数据填充方式
- iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
- 如何让UITableView的section headerView不悬停
- iphone开发之表格组件UITableView的使用(二)如何分组展示数据并添加组头和组尾描述
- IOS11以后,如何调整UITableView的section的高度
- iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section
- 如何禁止UITableView的section停留(去除卡住的效果)
- IOS中使用UITableView的group时,如何让第一行数据挨着顶部
- CoreData 从入门到精通(五)CoreData 和 TableView 结合
- UITableView判断reloadData加载数据已经结束
- CoreData 从入门到精通(五)CoreData 和 TableView 结合
- 如何在UITableView reloadData结束后再进行下一步操作
- Swift UITableView相关功能(二)填充数据
- UITableView 如何修改数据
- iOS-分组UITableView删除崩溃问题(当删除section中最后一条数据崩溃的情况)