【问题标题】:Add a tap event to CollectionViewCell while passing Cell data在传递 Cell 数据时向 CollectionViewCell 添加点击事件
【发布时间】:2018-02-04 19:23:12
【问题描述】:

我想向我的CollectionViewCell 添加一个点击事件,并将我的cell 与它拥有的数据一起传递到那里。我怎样才能做到这一点? 这个事件应该由我的ViewController 处理还是由CollectionViewCell 处理?

我的ViewController

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
    cell.imgImage.image = imageArray[indexPath.row]
    cell.url = "xhini"

    return cell
}

我的CollectionViewCell

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imgImage: UIImageView!
    var url: String = "url"
}

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    实现UICollectionViewDelegate,然后您可以在ViewController 中使用以下方法对选择单元格做出反应:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let image = imageArray[indexPath.row]
        // do stuff with image, or with other data that you need
    }
    

    别忘了在你设置数据源的地方设置委托:

    collectionView.dataSource = self
    // add this line:
    collectionView.delegate = self
    

    更新

    或者,如果您正在使用情节提要,您希望使用情节提要设置它,就像为dataSourcetableView 设置数据源一样:

    更新 2

    您的tap 手势识别器取消了集合视图的事件,因此要解决此问题,只需取消注释tap.cancelsTouchesInView = false 行,它就会起作用:

        //Looks for single or multiple taps.
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissKeyboard))
        //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    

    【讨论】:

    • @rexhin 不要忘记将代理设置为自己:collectionView.delegate = self
    • @rexhin 不,一点也不。我认为viewDidLoad 是最好的地方.. 找到你设置数据源的位置(你有collectionView.dataSource = self 行的位置),并将其添加到那里
    • @rexhin :我建议您阅读 Apple 文档,或者可能是一个很好的 UICollectionView 教程,或者就此而言,关于如何使用委托以及在何处设置委托的教程。
    • @rexhin 将其添加到viewDidLoad,或者通过故事板添加委托,方法是将集合视图的delegate 链接到viewController(与使用dataSource 的方式相同) - 我'将更新答案以包含屏幕截图
    • @rexhin :根据您共享的代码,没有 UICollectionView。我只猜我可以做的是,您直接从情节提要中绑定了 collectionView,而无需为此创建 IBOutlet。所以有两种方法:1.从storyboard中设置delegate和dataSource。 2. 将 IBOutlet 分配给 UICollectionView 并将其链接到故事板,然后按照米兰的建议设置委托和数据源
    【解决方案2】:

    我看到了您在@Milan 的上述答案中分享的代码,并找出了原因。
    您已在 ViewControllerviewDidLoad 上添加了一个点击手势:

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)  
    

    这使得UICollectionView's didSelectItemAt 不会被调用。
    所以评论这段代码,它应该可以工作。
    对于这个手势,你得另谋出路

    【讨论】:

    • 你建议他打破其他功能来解决他当前的问题。这有什么帮助?
    • 如果你想获得接受的答案,你可以得到它。我解决了OP的问题。关于手势,如果他遇到问题,我很乐意提供帮助。顺便说一句,应该担心的是OP :)
    • @rexhin :没问题。从执行的角度来看,米兰的回答是绝对正确的,应该被授予。很高兴我能帮上忙。
    • @rexhin :这应该对您有所帮助。检查接受的答案:stackoverflow.com/questions/40365971/…
    • 是的,在问这个问题之前我有这行,但由于某种原因它被注释掉了
    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多