Force UITableView to Scroll to Tops of Cells

后端 未结 6 2057
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 13:41

Is there a way to make UITableViews scroll cell to cell? That is, the top of the Table View is always the top of a UITableViewCell.

I tried the pageation flag, but t

6条回答
  •  庸人自扰
    2020-12-29 14:05

    This works well if you have sections and rows with static heights.

    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
        // Adjust target offset so that cells are snapped to top
        let cellHeight: CGFloat = 44
        let headerHeight: CGFloat = 30
        let section = (indexPathsForVisibleRows?.first?.section ?? 0) + 1
        targetContentOffset.memory.y -= (targetContentOffset.memory.y % cellHeight) - (CGFloat(sectionIndex) * headerHeight)
    }
    

提交回复
热议问题