第0部分中的硬编码Tablecells,第1部分中的核心数据获取结果

时间:2011-02-21 13:18:23

标签: objective-c uitableview core-data

我的问题是我有两个部分的UITableView。 在第一部分中,我有一个NSArray的对象,第二部分应该填充核心数据结果。

但是如果我使用fetchedResultsController,它会将他的数据部分设置为0,而不是根据需要设置为1。 所以cellForIndexPath使用indexPath,如果我想在第1节中显示核心数据,则fetchedResultsController不会找到数据。
我目前的解决方法是

if (indexPath.section == 1) {
    NSIndexPath *temp = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section-1];

然后使用temp而不是indexPath 但如果部分== 1,那么检查每个UITableView函数是非常难看的。

是否有更好的方法直接在第1部分中获取结果?

非常感谢! Seega

2 个答案:

答案 0 :(得分:0)

你的fetchedResultsController没有确定将要插入所提取数据的WHERE,这取决于你的tableview dataSource方法根据你放置的逻辑来计算。

我没有您实施的所有细节,但下面应该粗略地做您想要实现的目标。您只需要在代码的某些部分测试(部分)值,以确定如何填充部分和行的数据。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    if (indexPath == kSectionZero) {
          // Configure cell with data from NSArray here
    } else {
          NSManagedObject *yourObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
          // Configure cell with data from *yourObject here
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSUInteger count = [[self.fetchedResultsController sections] count];
    return count + X; // where X == number of additional sections you want, in your case X == 1
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects]
    } else return [arrayObject count]
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        NSString *sectionName = [[[self.fetchedResultsController sections] objectAtIndex:section] name];
        return sectionName;
        } else return @"Whatever section name you want for section == 0 here";
    }
}

答案 1 :(得分:0)

警告这是一个Kludge:

为您的Entity添加一个“sectionKludge”值,并为sectionKludge实例化该Entity的虚拟版本,值为0(忽略此ManagedObject,您将不会在表中看到它)然后实例化所有真实实体值为1(您甚至可以将此值设置为此可选属性的默认值)。在你的FRC中将sectionNameKeyPath设置为“sectionKludge”,然后使用Rog的答案。

我已经呻吟着在我写的应用程序中的几个表视图中做了你正在询问的事情......我通常只是退缩并手动愚弄数组而不使用FRC。

我只是像你说的那样调整NSIndexPath并完成它。

相关问题