【发布时间】:2017-06-03 00:56:05
【问题描述】:
是否可以通过在集合视图 Cell .swift 文件中编写代码来阻止我的集合视图滚动。我希望能够在用户点击单元格中的按钮时停止滚动,然后在再次按下按钮时允许滚动。
【问题讨论】:
标签: ios swift uicollectionview
是否可以通过在集合视图 Cell .swift 文件中编写代码来阻止我的集合视图滚动。我希望能够在用户点击单元格中的按钮时停止滚动,然后在再次按下按钮时允许滚动。
【问题讨论】:
标签: ios swift uicollectionview
为您的单元创建自定义委托
protocol CustomCellDelegate: class {
func cellDidSetScrolling(enabled: Bool)
}
class CustomCell: UICollectionViewCell {
var delegate: CustomCellDelegate?
// ....
}
将委托分配给cellForItem中的单元格
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// dequeue cell and assign delegate
var cell: CustomCell?
cell.delegate = self
return cell
}
在按钮操作上调用单元委托。使用button.tag 确定enabled 的值
func buttonAction() {
button.tag = button.tag == 0 ? 1 : 0 // toggle value
delegate?.cellDidSetScrolling(enabled: button.tag == 1)
}
在ViewController中实现委托
class ViewController: UIViewController, CustomCellDelegate {
func cellDidSetScrolling(enabled: Bool) {
collectionView.isScrollEnabled = enabled
}
}
编码愉快!
【讨论】:
0(我的意思是你不需要给它一个初始值。对)......所以一旦点击它,你就将它设置为1并基于此做出isScrollEnabled 的决定...您基本上是在滥用标签属性来进行此操作...