2017-06-02 90 views

回答

3

为你的创建自定义的委托

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

太棒了。这是什么意思,如果button.tag等于1? – Honey

+0

代码将切换isScrollEnabled的值。也就是说,如果滚动,如果启用,按钮点击将禁用它,反之亦然。 –

+0

我在问一些不同的东西。纠正我,如果我错了:默认情况下,按钮标记设置为'0'(我的意思是你不需要给它一个初始值,右)...所以一旦它被点击,你将它设置为'1'并根据你的决定做出“isScrollEnabled”......你基本上在滥用标签属性来做这个操作...... – Honey

相关问题