开发者

hardcoded Tablecells in section 0, core-data fetched-results in section 1

my problem ist that i have UITableView with two sections. In the first section i have objects of a NSArray and the second section should be filled with core-data results.

But if i use the fetchedResultsController it sets the section of his data to 0 and not to 1 as i need.

So cellForIndexPath uses the indexPath, and if i want to display the core-data in section 1 the fetchedResultsController doesn't find data.

My current workaround is

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

and then use temp instead of indexPath.

But it's very ugly to check in every UITableView开发者_开发知识库 function if the section==1.

Is there a better way to get the fetched results directly in section 1?

Thank you very much! Seega


Warning this is a Kludge:

add a "sectionKludge" value to your Entity and instantiate a dummy version of this Entity with a value of 0 for sectionKludge (ignore this ManagedObject, you will not see it in your table) and then instantiate all of the real Entity's with a value of 1 (you could even make this the default value for this optional property). In your FRC set the sectionNameKeyPath to "sectionKludge" and then use Rog's answer.

I have groaned about doing what you are asking in several of the table views that I have in apps that I write... I usually just back down and manually fool with the array and not use an FRC.

I would just adjust the NSIndexPath like you are saying and be done with it.


Your fetchedResultsController doesn't determine WHERE the fetched data is going to be inserted, this is up to your tableview dataSource methods to work out based on logic that you put in place.

I don't have all the details of your implementation but the below should roughly do what you are trying to achieve. You simply need to test for the (section) value in certain parts of your code to work out how you are going to fill the data for sections and rows.

- (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";
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜