我正在使用基于导航的CoreData模板开发iOS应用。 我希望在插入表格视图后选择并“滚动到可见”一行。理想情况下,我想选择它,取消选择它并再次选择它,以获得一种闪烁效果。
当我使用该方法时,模板提示,即:
#pragma mark - Fetched results controller delegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type)
{
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
我有点困惑,不知道,在哪里放选择代码。
如果我放了一个
[tableView selectRowAtIndexPath:<#(NSIndexPath *)#> animated:<#(BOOL)#> scrollPosition:<#(UITableViewScrollPosition)#>]
到
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
,
它选择了行,但是立即取消选择它,滚动的行为也不应该如此。
答案 0 :(得分:3)
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
中的改变这一部分:
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView scrollToRowAtIndexPath:newIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
insertedIndexPath = newIndexPath; //remember for selection
break;
并从UIScrollViewDelegate添加方法:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[self.tableView selectRowAtIndexPath:insertedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
记得添加insertedIndexPath变量。
答案 1 :(得分:3)
我解决了类似的问题。我将项目插入表格的开头,我想立即选择它。
步骤1. - 如果要选择第一行,则创建一个保持状态的开关
@implementation MyTableViewController {
BOOL _selectFirstRow;
}
步骤2. - 插入
时将其切换为YES- (void) controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
_selectFirstRow = YES;
break;
步骤3. - 创建选择第一行的方法
- (void) selectFirstTableRow
{
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:YES
scrollPosition:UITableViewScrollPositionTop];
}
步骤4. - 调用方法以在表更新后立即选择行
- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
if (_selectFirstRow) {
[self selectFirstTableRow];
_selectFirstRow = NO;
}
}
这就是所有人..如果你想选择不同的行,也要在class参数中存储newIndexPath。就这样。我希望它会对你有所帮助。 CIA!
答案 2 :(得分:2)
与上面大致相同的答案,但更正了何时调用scrollToRowAtIndexPath
方法controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
中的改变这一部分:
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//save new index path into your own ivar
self.newlyInsertIndex = newIndexPath
break;
如果使用锅炉板FRC代码,则在表更新完成之前无法滚动到新插入的索引
在方法- (void)controllerDidChangeContent:
if (!self.changeIsUserDriven){
[self.tableView endUpdates];
if (self.newlyInsertedIndex){
//ScrollPositionNone == use least amount of effort to show cell
[self.tableView scrollToRowAtIndexPath:self.newlyInsertedIndex
atScrollPosition:UITableViewScrollPositionNone animated:YES];
//cleanup
self.newlyInsertedIndex = Nil;
}
}
并从UIScrollViewDelegate添加方法:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[self.tableView selectRowAtIndexPath:insertedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
记得添加newInsertedIndex ivar
答案 3 :(得分:0)
这一切都不适合我。我做的是:
_newInstance
_newInstance = nil;
controllerDidChangeObject
我选择了行:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
if (_newObject) {
NSIndexPath *ip = [self.fetchedResultsController indexPathForObject:_newObject];
[self.tableView selectRowAtIndexPath:ip animated:YES
scrollPosition:UITableViewScrollPositionMiddle];
}
}