gpt4 book ai didi

ios - UITableview 的滑动手势

转载 作者:行者123 更新时间:2023-12-01 21:59:11 25 4
gpt4 key购买 nike

我有一种感觉,答案将是“你不能那样做”,但希望永远存在!

我的应用程序中的一个场景是 UITableView;该表代表一个月,每一行是一个月中的一天。我在表格顶部有一个带有“Prev”和“Next”按钮的小 UIView。在显示下个月的表格中选择下一个结果,Prev 会显示上个月。所有这些都很好。

我想做的是添加左右手势识别器以逐月移动,执行与 Prev 和 Next 按钮相同的功能。请注意,我不需要 UITableViewCell 发生任何事情。

当我在网上搜索这个时,绝大多数帖子似乎是如何处理 UITableViewCell 中的手势识别器。这对我来说没有帮助。

我正在使用 Objective-C 和 Xcode 11.3.1。

有任何想法吗?

提前致谢。

最佳答案

你应该能够做到。通过禁用每个单元格的交互,您应该能够将“UIGestureRecognizer”传递给 tableView。

这是一个简短的例子。抱歉,我使用了 Swift,但您可能可以了解它的要点。

添加 tableView 后,我向其中添加了这些手势识别器。

let leftGesture = UISwipeGestureRecognizer(target: self, action: #selector(leftSwiped))
leftGesture.direction = .left
tableView.addGestureRecognizer(leftGesture)

let rightGesture = UISwipeGestureRecognizer(target: self, action: #selector(rightSwiped))
rightGesture.direction = .right
tableView.addGestureRecognizer(rightGesture)

每个都调用以下函数:
@objc func leftSwiped() {
print("LEFT")
}

@objc func rightSwiped() {
print("Right")
}

在行的单元格中,我刚刚禁用了单元格的用户交互:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.isUserInteractionEnabled = false
cell.backgroundColor = .random()
return cell
}

当我尝试它时,在表格 View 上左右滑动会在记录器中正确打印。

希望这可以帮助,
艾伦

关于ios - UITableview 的滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com