ios – 插入后选择行的最佳方法?

前端之家收集整理的这篇文章主要介绍了ios – 插入后选择行的最佳方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用基于导航的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 *)#>动画:其中#(BOOL)#>的scrollPosition:其中#(UITableViewScrollPosition)#>]

– (void)controllerDidChangeContent:(NSFetchedResultsController *)控制器,
它会选择行,但会立即取消选择它,并且滚动操作也不应该如此.

解决方法

方法控制器中: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变量.

原文链接:https://www.f2er.com/iOS/334094.html

猜你在找的iOS相关文章