[英]Swift swipe between table view cells?
Currently, I have a tableView
where each cell occupies the entire safe area, so the entire phone screen will be filled with the cell. 目前,我有一个
tableView
,其中每个单元格都占据了整个安全区域,因此整个电话屏幕将被单元格充满。 However , rather than scrolling between the cells, how could I get it so that the users could Swipe Up/Down between the cells (no scrolling , just swiping up and down between the table view cell so one cell will come from the bottom and completely replace the cell on the screen) I am using dequeable cells - I have tried turning off the scroll and using the cellWillload, however , I am not getting the desired results of swiping up and down between the cells. 但是,而不是在单元格之间滚动,我如何获得它,以便用户可以在单元格之间向上/向下滑动(不滚动,仅在表格视图单元格之间向上或向下滑动,这样一个单元格将从底部完全替换屏幕上的单元格)我使用的是可变形单元格-我尝试关闭滚动并使用cellWillload,但是,在单元格之间上下滑动并没有达到预期的效果。
In your table view's delegate (probably your view controller), override scrollViewWillEndDragging:withVelocity:targetContentOffset:
to set targetContentOffset
so just one cell is visible. 在表视图的委托(可能是视图控制器)中,重写
scrollViewWillEndDragging:withVelocity:targetContentOffset:
设置targetContentOffset
以便仅显示一个单元格。
Example: 例:
class ViewController: UITableViewController {
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let targetRect = CGRect(origin: targetContentOffset.pointee, size: tableView.bounds.size)
let indexPaths = tableView.indexPathsForRows(in: targetRect) ?? []
let target: Int
switch (indexPaths.count, velocity.y.sign) {
case (0, _): return
case (1, _): target = 0
case (_, .plus): target = 1
case (_, .minus): target = 0
}
targetContentOffset.pointee.y = tableView.rectForRow(at: indexPaths[target]).origin.y - tableView.adjustedContentInset.top
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let desiredRowHeight = tableView.frame.size.height - tableView.adjustedContentInset.top - tableView.adjustedContentInset.bottom
if tableView.rowHeight != desiredRowHeight {
tableView.rowHeight = desiredRowHeight
tableView.beginUpdates()
tableView.endUpdates()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 20 }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
cell.indexPath = indexPath
return cell
}
}
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.