但无法滚动到正确位置

但无法滚动到正确位置

这是我的代码如下:

 if (![self.cityLabel.text isEqualToString:@""] && dataOfCity.count != 0) {
        for (int i = 0; i < dataOfCity.count; i ++) {

            if ([dataOfCity[i] isEqualToString:self.cityLabel.text]) {

                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_tab_city selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
            }
        }

    }


result是不在以下tableView中间的图片:

ios - iOS:将scrollPosition设置为“UITableViewScrollPositionMiddle”,但无法滚动到正确位置-LMLPHP

最佳答案

I think I understand what happens:
the table view is already performing another animation and the scrollToRowAtIndexPath is ignored.
Wrapping the method in CATransaction block works for me (swift code):

    CATransaction.begin()

    tableView.beginUpdates()
    tableView.scrollToRowAtIndexPath(cell.indexPath, atScrollPosition: .Middle, animated: true)
    tableView.endUpdates()

    CATransaction.commit()

    Another solution that worked for me was using performSelectorWithDelay, with the delay being long enough for other animations on the table view to finish.

关于ios - iOS:将scrollPosition设置为“UITableViewScrollPositionMiddle”,但无法滚动到正确位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38098871/

10-11 05:07