11

链接到 UITableView 的子类

为什么 UITableView 滚动时会跳转?你能帮助我吗?

对于帮助我的人,我开始并稍后奖励 100 的另一个赏金:-)

如何在UITableView设置它的同时执行一些更改contentOffset

这就是我设置我的方式scrollDisplayLink

scrollDisplayLink = CADisplayLink(target: self, selector: Selector("scrollTable"))
scrollDisplayLink?.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes)
cellForCurrentIndexPath?.hidden = true

然后在scrollTable我做以下事情:

func scrollTable() {
    var newOffset = CGPointMake(currentContentOffset.x, currentContentOffset.y + scrollRate * 10)

    if currentContentSize.height < frame.size.height {
        newOffset = currentContentOffset
    } else if newOffset.y > currentContentSize.height - frame.size.height {
        newOffset.y = currentContentSize.height - frame.size.height
    } else if newOffset.y < 0 {
        newOffset = CGPointZero
    }

    contentOffset = newOffset


    if coveredIndexPath != nil && coveredIndexPath! != currentIndexPath! {

        let verticalPositionInCoveredCell = longPressGestureRecognizer.locationInView(cellForRowAtIndexPath(coveredIndexPath!)).y

        if direction == .Down && heightForCoveredCell - verticalPositionInCoveredCell <= heightForCurrentCell / 2 {
                print("moved down")

                beginUpdates() //1
                moveRowAtIndexPath(currentIndexPath!, toIndexPath: coveredIndexPath!)  //2
                currentIndexPath = coveredIndexPath //3
                endUpdates() //4

        }
    }
}

滚动很好,除非行1-4被注释或我禁用UITableViewAutomaticDimension. 当他们没有被评论时,表格跳转的时间与滚动时currentContentOffset不同。0为什么会这样?是线程问题还是其他问题?

笔记:

UITableView适用于UITableViewAutomaticDimension

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
4

1 回答 1

0

让我们从显而易见的开始:

UITableView 是 UIScrollView 的子类,我们可以使用它的一些属性。

几乎每年都有一个很棒的 WWDC 讨论如何自定义 UIScrollviews。

他们建议做你在里面做的事情

// is called every frame of scrolling and zooming
override func layoutSubviews() {
    ...
    super.layoutSubviews()
    ...
}

在用户滚动时调用每一帧。

我在滚动时大量使用这个地方来处理 currentContentOffset 以及大小和坐标系统(我继承了 UIScrollView 而不是 UITableView)。

优点:

  • 您可以免费获得每一帧的回调,无需使用 CADisplayLink
  • 当 UIScrollView 期望更改内容时,您正在更改内容偏移量。

希望这可以帮助

附言

根据我的实验,在 UIScrollView 内滚动的视图不能高于 8000 像素左右。

所以苹果的 UITableView 必须实现一种称为无限滚动的策略,并且在一个关于 UIScrollViews 的 WWDC 视频中有所描述:

简而言之:

表格的一部分被绘制一次,然后滚动而不重绘表格内容。当用户滚动太远时,滚动表格的不同部分被绘制,同时 contentOffset 被 UITableView 实现更改。

这可能在您在 layoutSubviews() 的实现中调用 super.layoutSubviews() 时完成。

于 2015-10-21T14:54:00.367 回答