- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 UITableView 和 NSFetchedResultsController 附加到它。 FRC 的委托(delegate)方法完美运行,一切都很好,但我的单元格具有取决于单元格位置的自定义背景:1. 在 UITableView 的顶部(顶部有圆角的背景)2.在中间(没有圆角的背景)3.在底部(底部有圆角的背景)4. 单格(所有角都是圆的)。在我的单元格配置方法中,我计算了单元格的位置,并为其设置了适当的背景 View 。一切正常,但是FRC的delegate方法的实现出现了问题:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.agenciesTableView;
switch(type)
{
case NSFetchedResultsChangeInsert:
{
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
break;
}
case NSFetchedResultsChangeDelete:
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
break;
}
case NSFetchedResultsChangeUpdate:
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
case NSFetchedResultsChangeMove:
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
};
}
如您所见,这很常见,但有一个问题:如果单元格被插入到 UITableView 的顶部,它的配置方法 (cellForRowAtIndexPath) 被调用并且它正确绘制(带有圆顶角)但是它下面的单元格(以前在顶部)仍然带有圆顶角和我需要以某种方式重新绘制它。
-=已编辑=-
我更改了 FRC 的委托(delegate)方法以反射(reflect)更新逻辑:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.agenciesTableView;
switch(type)
{
case NSFetchedResultsChangeInsert:
{
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
//if we inserting a cell not to the middle of table view
//we need to update the previous cell (if we are inserting to the end)
// or the next cell (if we are inserting to the beggining)
if ([self controller:controller positionForCellAtIndexPath:newIndexPath]!=UITableViewCellPositionMiddle)
{
NSIndexPath *prevIndexPath=[NSIndexPath indexPathForRow:(newIndexPath.row+1) inSection:newIndexPath.section];
NSIndexPath *nextIndexPath=[NSIndexPath indexPathForRow:(newIndexPath.row-1) inSection:newIndexPath.section];
if ([self controller:controller cellExistsAtIndexPath:prevIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
if ([self controller:controller cellExistsAtIndexPath:nextIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
};
break;
}
case NSFetchedResultsChangeDelete:
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
//if we deleting a cell not fromo the middle of table view
//we need to update the previous cell (if we are deleting from the end)
//or the next cell (if we are deleting from the beggining)
if ([self controller:controller positionForCellAtIndexPath:indexPath]!=UITableViewCellPositionMiddle)
{
NSIndexPath *prevIndexPath=[NSIndexPath indexPathForRow:(indexPath.row+1) inSection:indexPath.section];
NSIndexPath *nextIndexPath=[NSIndexPath indexPathForRow:(indexPath.row-1) inSection:indexPath.section];
if ([self controller:controller cellExistsAtIndexPath:prevIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
if ([self controller:controller cellExistsAtIndexPath:nextIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
};
break;
}
case NSFetchedResultsChangeUpdate:
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
}
case NSFetchedResultsChangeMove:
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
if ([self controller:controller positionForCellAtIndexPath:indexPath]!=UITableViewCellPositionMiddle)
{
NSIndexPath *prevIndexPath=[NSIndexPath indexPathForRow:(indexPath.row+1) inSection:indexPath.section];
NSIndexPath *nextIndexPath=[NSIndexPath indexPathForRow:(indexPath.row-1) inSection:indexPath.section];
if ([self controller:controller cellExistsAtIndexPath:prevIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
if ([self controller:controller cellExistsAtIndexPath:nextIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
};
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
if ([self controller:controller positionForCellAtIndexPath:newIndexPath]!=UITableViewCellPositionMiddle)
{
NSIndexPath *prevIndexPath=[NSIndexPath indexPathForRow:(newIndexPath.row+1) inSection:newIndexPath.section];
NSIndexPath *nextIndexPath=[NSIndexPath indexPathForRow:(newIndexPath.row-1) inSection:newIndexPath.section];
if ([self controller:controller cellExistsAtIndexPath:prevIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
if ([self controller:controller cellExistsAtIndexPath:nextIndexPath])
{
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:nextIndexPath] withRowAnimation:UITableViewRowAnimationNone];
};
};
break;
}
};
}
但在这种情况下我得到一个错误:
2013-05-24 21:04:19.458 PharmaTouch[6994:fb03] *** Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit_Sim/UIKit- 1912.3/UITableViewSupport.m:386
2013-05-24 21:04:19.483 PharmaTouch[6994:fb03] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source. with userInfo (null)
-=EDITED2=-这是我的 controller:cellExistsAtIndexPath: 方法实现。它用于安全检查,以避免使用超出范围的 indexPath 参数调用 reloadRowsAtIndexPaths。
-(BOOL)controller:(NSFetchedResultsController *)controller cellExistsAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row<0)
{
return NO;
};
NSInteger numberOfRows = 0;
NSArray *sections = controller.sections;
if(sections.count > 0)
{
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:indexPath.section];
numberOfRows = [sectionInfo numberOfObjects];
};
if (indexPath.row>(numberOfRows-1))
{
return NO;
}
else
{
return YES;
};
}
最佳答案
当您使用 UITableViewRowAnimationNone
时,您也可以只使用 reloadData
然后它会正确完成,因为所有行都将被刷新。
在你使用 UITableViewRowAnimationFade
的地方,考虑一下你对它的重视程度......
您需要在检查和重新加载方面更加全面。特别是,您需要从 FRC 获取正在更改的部分的部分信息。如果传入或传出行是第一行或最后一行,则需要刷新多行。沿着:
if first is changing and count > 1, also refresh second
if last is changing and count > 1, also refresh first
关于iphone - NSFetchedResultsController 的委托(delegate)方法处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740610/
我正在尝试使用具有混合语言数据的 FRC,并希望有一个部分索引。 从文档看来您应该能够覆盖 FRC - (NSString *)sectionIndexTitleForSectionName:(NSS
使用等效的 NSFetchRequest 重新创建 NSFetchedResultsController 时,为什么 [NSFetchedResultsController PerformFetch:
我正在尝试起草一个可重复使用的类,该类基本上可重复用于具有几乎相同实现的多个实体。基本上我将有几个 UIViewController,每个都包含 UIView,其中包含 UITableView 以及由
我有一个 iPhone 和一个 Android 应用程序。在 iPhone 上,我使用 NSFetchedResultsController 来管理我的 UITableView 的数据。目前我有很多数
NSFetchedResultsController 监视对正在跟踪的整个托管对象的更改。每当在当前上下文中修改任何属性时,例如 – controller:didChangeObject:atInde
自 1 个月以来,我一直在使用 NSFetchedResultsController 构建应用程序,并在 3.1.2 SDK 上测试该应用程序。问题是我一直在我的应用程序中到处使用 NSFetched
我有一个人物和宠物的数据库,具有一对多关系 人宠物 姓名 姓名 宠物>主人 我正在使用由核心数据支持的 UITableView 和 nsfetchedresultscontroller 来显示宠物列表
我有一个 NSFetchedResultsController,它在 TableView 中显示项目列表,包括关联实体的计数。当为此关联添加对象时(使用 addXXXObject),不会调用回调来通知
花了几个小时后,我发现无法在以下文章中由 SQLite 支持的 NSFetchedResultsController 中进行自定义排序。 NSFetchedResultsController cust
我设置了一个 NSFetchedResultsController 来填充 UITableView,基于我的“主”NSManagedObjectContext。 在计时器中,我不断地将对象添加到单独的
在我的应用程序中,我需要循环遍历核心数据中的所有实体,并且我正在使用 NSFetchedresultcontroller。 我现在正在这样做: NSArray *tempArray = [[NSArr
我正在开发我的第一个核心数据支持的应用程序,但无法弄清楚如何正确设置 NSFetchedResultsController。我有两个实体: /-----------\ /--------
我正在使用 NSFetchedResultsController 在 TableView 中显示项目: - (NSInteger)numberOfSectionsInTableView:(UITabl
我将 NSFetchedResultsController 放入我的代码中,这样我就可以对 TableView 数据进行很好的自动分段。 所以我正在运行测试以确保一切正常。我的持久存储中有一个 Boo
我如何使用带有翻译后的排序键和sectionKeyPath的NSFetchedResultsController? 问题:我在数据库中的属性“type”中有 ID,例如 typeA、typeB、typ
大家好 我的 CoreData 驱动应用程序有多个来自同一存储的数据 View ,并且全部使用 NSFetchedResultsController。目前,它们的 NSFetchedResultsCo
我想使用使用 bool 值设置的不同谓词从 NSFetchedResultsController 重新获取数据。如何刷新 NSFetchedResultsController 以获取一组新数据? -
核心数据在 NSManagedObjectContext 类中提供了“executeFetchRequest”方法,我们可以使用该方法从表中获取数据并以任何需要的方式使用它。 现在还有另一种方法,即使
我正在编写一个应用程序,一个屏幕上有两个表格。左表是文件夹列表,右表显示文件列表。当点击左侧的一行时,右侧表格将显示属于该文件夹的文件。 我正在使用 Core Data 进行存储。当文件夹的选择发生变
我有一个由 NSFetchedResultsController 管理的 TableView 。但是,我在查找或创建操作时遇到问题。当用户点击表格 View 的底部时,我正在向服务器查询另一批内容。如
我是一名优秀的程序员,十分优秀!