gpt4 book ai didi

ios - 使用 NSFetchedResultsController 在空表上自定义单元格

转载 作者:可可西里 更新时间:2023-11-01 05:03:19 25 4
gpt4 key购买 nike

当我的 UITableView 为空时,我试图放置一个自定义的空单元格。

我使用以下代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

int count = [self.fetchedResultsController fetchedObjects].count;
if (count == 0) {
return 1;
}
return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = nil;
int count = [self.fetchedResultsController fetchedObjects].count;
if (count == 0) {
// return empty cell
cell = [self getEmptyCellOfTableView:tableView cellForRowAtIndexPath:indexPath];
} else {
cell = [self getMyQuestionCellOfTableView:tableView cellForRowAtIndexPath:indexPath];
}
return cell;
}

FRC didChangeObject 实现:

- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}

这非常有效,问题是当 fetchedObject.count > 0 时出现以下错误:

CoreData: error: Serious application error.  
An exception was caught from the delegate of NSFetchedResultsController during a call to -
controllerDidChangeContent: Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (1)
must be equal to the number of rows contained in that section before the update (1),
plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted)
and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
with userInfo (null)

我知道发生这种情况是因为我插入了 1 个新行并且计数保持在 1 而不是变成 2。

我该如何修正它以适应我的行为?

最佳答案

TableView 更新后,numberOfRowsInSection 返回的值必须完全 匹配前面的行数加上插入的行数减去行数删除的行。

在您的情况下,当插入第一个对象时,在 FRC 委托(delegate)方法中为新对象调用 insertRowsAtIndexPaths,但不是显示额外的行,而是显示“空”单元格被替换为不同的单元格,因此行数仍然是一。

修改FRC委托(delegate)方法应该可以 Controller :didChangeObject:atIndexPath:forChangeType:newIndexPath:如下:

case NSFetchedResultsChangeInsert:
if ([[self.fetchedResultsController fetchedObjects] count] == 1) {
// First object inserted, "empty cell" is replaced by "object cell"
[tableView reloadRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
} else {
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;

case NSFetchedResultsChangeDelete:
if ([[self.fetchedResultsController fetchedObjects] count] == 0) {
// Last object removed, "object cell" is replaced by "empty cell"
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;

关于ios - 使用 NSFetchedResultsController 在空表上自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17761221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com