使用 NSFetchedResultsController 锚定一行
我正在尝试使我的 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>....
}
I'm trying to make it so my UITableView
always has a specific row at the bottom, anchored there.
I'm using a NSFetchedResultsController
to perform a fetch, and then the regular Apple boilerplate for detecting a merged context change.
What I'd like to do is always have one row at the bottom of the results, "Not What You're Looking For?". How would I do this? I'm comfortable with custom cell types, but I can't get even a cell of the same type to anchor to the bottom.
Code that adds one more row than what is in the fetchedResultsController:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return[sectionInfo numberOfObjects]+1;
}
Code for 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;
}
This would work if not using a NSFetchedResultsController, but what happens is that whenever a cell is updated, the method 'configureCell' is being called from the Controller, which knows nothing about SpecialCell.
From - (void)controller:(NSFetchedResultsController *)controller didChangeObject:......
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
And here is 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>....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
cellForRowAtIndexPath
中,当您为数组中的不同行创建单元格时,请保留以下条件:如果它到达数组末尾,则您可以使用所需的字符串添加自定义单元格...了解更多信息请添加代码...
in
cellForRowAtIndexPath
when you are creating cell for different rows from array, then keep the condition that if it reaches the end of array then you add your custom cell with a string that you want...For more information please add the code...