Ios 如何阻止UITableview一次滚动多个单元格
因此,我的每个tableview单元格都具有与tiktok、igtv等类似的全屏高度。当用户滚动时,我希望tableview在每个单元格处停止,而不是滚动一次并通过2/3个单元格 我以编程方式使用自定义tableview单元格,这就是我目前实现tableview委托和数据源函数的方式Ios 如何阻止UITableview一次滚动多个单元格,ios,swift,uitableview,Ios,Swift,Uitableview,因此,我的每个tableview单元格都具有与tiktok、igtv等类似的全屏高度。当用户滚动时,我希望tableview在每个单元格处停止,而不是滚动一次并通过2/3个单元格 我以编程方式使用自定义tableview单元格,这就是我目前实现tableview委托和数据源函数的方式 override func viewDidLoad() { super.viewDidLoad() tableView.register(PostViewTableCell.self, forC
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(PostViewTableCell.self, forCellReuseIdentifier: PostViewTableCell.cellReuseIdentifier)
tableView.allowsSelection = false
tableView.bounces = false
}
override var prefersStatusBarHidden: Bool {
return true
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModel?.postInformations.count ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: PostViewTableCell.cellReuseIdentifier) as? PostViewTableCell else {
fatalError("Couldn't dequeue reusable cell")
}
cell.postViewCellOutput = self
cell.setUpCell(position: indexPath.row, viewModel: viewModel)
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.size.height
}
因此,我在手机上调用canPlay来播放屏幕上完全可见的手机,以便它可以开始播放。我正在使用此方法检查可见性:
private func checkIfCellIsVisible(indexPath: IndexPath) -> Bool {
let cellRect = tableView.rectForRow(at: indexPath)
return tableView.bounds.contains(cellRect)
}
您可以在UITableViewDelegate上使用UIScrollViewDelegate的
func ScrollViewWillendDraging(uquot:withVelocity:targetContentOffset:)
,因为它也是一个滚动视图委托,用于检测拖动结束并将targetContentOffset设置为您想要的行,在本例中是下一行。例如:
var currIndexPath = IndexPath(row: 1, section: 0)
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let rect = tableview.rectForRow(at: self.currentIndexPath)
targetContentOffset.pointee = CGPoint(x: rect.minX, y: rect.minY)
self.currentIndexPath = IndexPath(row: self.currentIndexPath.row+1, section: 0)
}
var currendexpath=IndexPath(行:1,节:0)
func ScrollViewWillendDraging(scrollView:UIScrollView,withVelocity:CGPoint,targetContentOffset:UnsafemeutablePointer){
让rect=tableview.rectForRow(at:self.currentIndexPath)
targetContentOffset.pointee=CGPoint(x:rect.minX,y:rect.minY)
self.currentIndexPath=IndexPath(行:self.currentIndexPath.row+1,节:0)
}
这里的优点是,行的高度是全屏的,因此可以确保拖动在当前显示的行中结束。如果行高不是全屏,这将无法正常工作,因为您会看到滚动经过几个单元格,但最终会转到您想要的单元格。感谢您的回复。下面是我如何解决它的。我设定:
tableView.isScrollEnabled = false // Set the tableview scroll enabled to false
然后在“cellForRowAt indexPath:indexPath”函数中,我将第一行的indexPath保存到currentIndexPath属性:
currentIndexPath = indexPath
然后,我在tableview中添加了一个手势识别器,用于向上滑动:
let swipeToNextVideoGesture = UISwipeGestureRecognizer(target: self, action:
#selector(swipeToNext(_:)))
swipeToNextVideoGesture.direction = .up
tableView.addGestureRecognizer(swipeToNextVideoGesture)
然后在函数中,我首先检查下一行是否在tableview数据源数组边界中,然后滚动到该行:
@objc private func swipeToNext(_ gesture: UISwipeGestureRecognizer) {
let nextVideoIndexPath = currentIndexPath.row+1
if !(nextVideoIndexPath >= viewModel.postInformations.count) {
currentIndexPath = IndexPath(row: nextVideoIndexPath, section: indexPathSection)
tableView.scrollToRow(at: currentIndexPath, at: .none, animated: true)
}
}
这回答了你的问题吗?不,我检查了那个问题,他们没有解决方案。你试过公认的答案吗?它看起来确实正确(虽然我没有尝试过),但它不起作用。它仍然允许我滚动一次,并将继续滚动一个单元格。当用户登陆控制器时,它只起作用一次。当我滚动回到上面的项目时,它不起作用。