如何结合CoreData给UITableView的section填充数据
来源:互联网 发布:数据监控系统 编辑:程序博客网 时间:2024/06/08 05:59
搬运自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; }}
- 如何结合CoreData给UITableView的section填充数据
- UITableView,UICollectionView和CoreData完美结合进行增删改操作,如何导入大量数据,如何利用NSEntityMigrationPol
- 要用 CoreData 结合 UITableView 使用
- 如何获取UITableView 每个section的Rect
- coredata 自动化刷新uitableview数据
- IOS11以后,如何调整UITableView的section的高度
- 如何让UITableView的section headerView不悬停
- 给Repeater填充数据
- Oracle数据库中,如何给已有很多数据的表填充自增序列字段
- ios Coredata 关联 UITableView 数据自动更新
- NSFetchedResultsController和UITableView显示CoreData的数据时用relationship分组的方 ...
- 如何填充 TreeView 控件的 XML 数据
- CoreData的数据迁移
- CoreData的数据迁移
- 存储过程给变量填充数据的方式
- 自定义Section:修改UITableView的Section的背景和字体
- 自定义Section:修改UITableView的Section的背景和字体
- 如何禁止UITableView的section停留(去除卡住的效果)
- 无光驱无U盘 硬盘安装操作系统 NT6
- java 将远程共享的文件复制到本地
- tomcat在win7下面启动的问题。 郁闷了半天。 多亏了这位大哥。
- BouncyCastle JCE 的安装配置及在java中的使用
- shareperference使用
- 如何结合CoreData给UITableView的section填充数据
- HierarchicalStateMachine(HandlerStateMachine)分析
- 什么是极性反转 polarity reversal
- hbase 结点之间时间不一致造成regionserver启动失败
- 手动修改/etc/shadow和/etc/passwd中的用户密码
- 【转载】会议是浪费工作时间的最佳去处
- jQuery数组处理详解
- 20130324
- 谨慎验证码识别 还游戏账号一个安全的环境