【问题标题】:Anchor a row with NSFetchedResultsController使用 NSFetchedResultsController 锚定一行
【发布时间】:2011-11-06 17:08:06
【问题描述】:

我正在努力让我的UITableView 总是在底部有一个特定的行,固定在那里。

我使用NSFetchedResultsController 执行提取,然后使用常规 Apple 样板来检测合并的上下文更改。

我想要做的是始终在结果底部显示一行,“不是您要查找的内容?”。我该怎么做?我对自定义单元格类型感到满意,但我什至无法将相同类型的单元格锚定到底部。

比 fetchedResultsController 中的内容多添加一行的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return[sectionInfo numberOfObjects]+1;
}

cellForRowForIndexPath 的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifierNormal = @"NormalCell";

UITableViewCell *cell = nil;
cell = [tableView CellIdentifierNormal];

    if (cell == nil) {
        cell = [[NormalCell createCell]autorelease];
    }
// For regular results, go configure the cell. For the extra 'Special Row' at bottom, assign it the Special cell.
if([indexPath indexAtPosition:0] <= [[[fetchedResultsController sections] objectAtIndex:0] numberOfObjects])
     [self configureCell:cell atIndexPath:indexPath];
else {
    if (cell == nil) {
        cell = [[SpecialCell createCell] autorelease];
    }
    SpecialCell *nc = (SpecialCell*)cell;
    nc.labelFirstLine.text = @"Not What You're Looking For?";
}
return cell;
}

如果不使用 NSFetchedResultsController,这将起作用,但发生的情况是,每当更新单元格时,都会从控制器调用方法 'configureCell',而控制器对 SpecialCell 一无所知。

来自 - (void)controller:(NSFetchedResultsController *)controller didChangeObject:......

 case NSFetchedResultsChangeUpdate:
        [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
        break;

这里是configureCell:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell
// Go get it from coredata
    NormalCell *vc = (NormalCell*)cell;
NormalObject *no = (NormalCell *)[fetchedResultsController objectAtIndexPath:indexPath];
....<ASSIGNMENT TO CELL HERE>....

}

【问题讨论】:

  • 如需更好的答案,请发cellForRowAtIndex的代码。

标签: ios uitableview anchor nsfetchedresultscontroller


【解决方案1】:

cellForRowAtIndexPath 中,当您为数组中的不同行创建单元格时,请保持这样的条件,即如果它到达数组的末尾,则使用您想要的字符串添加自定义单元格...

更多信息请添加代码...

【讨论】:

  • 更新了新代码。您的方法适用于直接的 UITableView。但这是针对接收新数据的 CoreData 上下文,并且 NSFetchedResultsController 正在调用 configureCell。我不知道如何区分 ConfigureCell 中的细胞类型(NormalCell、SpecialCell)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多