是否可以通过在集合视图单元格.swift文件中编写代码来阻止我的集合视图滚动。当用户点击单元格中的按钮时,我希望能够停止滚动,然后在再次按下按钮时允许滚动。
发布于 2017-06-02 16:56:05
为单元格创建自定义委托
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
}
}编码愉快!
https://stackoverflow.com/questions/44333449
复制相似问题