在UICollectionViewCell中隐藏视图

时间:2016-05-12 15:00:26

标签: xcode swift uicollectionview cell

任何人都可以告诉我它不会在模拟器中隐藏我的UIView吗? 代码正在运行,它会打印我添加到函数中的数字,但它仍然不起作用。

基本上我想点击单元格,单元格中的greyView应该会消失。

 override func viewDidLoad() {
        super.viewDidLoad()

        let lpgr = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))
        lpgr.numberOfTapsRequired = 1
        lpgr.delaysTouchesBegan = true
        lpgr.delegate = self
        AlarmCollection.addGestureRecognizer(lpgr)

    }


    // MARK: UICollectionViewCell

    func handleTap(gestureReconizer: UITapGestureRecognizer) {
        if gestureReconizer.state != UIGestureRecognizerState.Ended {
            return
        }

        let p = gestureReconizer.locationInView(AlarmCollection)
        let indexPath = AlarmCollection.indexPathForItemAtPoint(p)

        if let index = indexPath {
            var cell = AlarmCollection.cellForItemAtIndexPath(index)
            // do stuff with your cell, for example print the indexPath
            print(index.row)
            updateView(indexPath!)
        } else {
            print("Could not find index path")
        }
    }



    func updateView(indexPath: NSIndexPath) {
        let cell = AlarmCollection.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! AlarmItemCell
        cell.greyView.hidden = true
        print("2")

    }

编辑:

// MARK: UICollectionViewCell



func handleTap(gestureReconizer: UITapGestureRecognizer) {
    if gestureReconizer.state != UIGestureRecognizerState.Ended {
        return
    }

    let p = gestureReconizer.locationInView(AlarmCollection)
    let indexPath = AlarmCollection.indexPathForItemAtPoint(p)

    if let index = indexPath {
        var cell = AlarmItemCell()
        // do stuff with your cell, for example print the indexPath
        print(index.row)
        updateView()
    } else {
        print("Could not find index path")
    }
}




func updateView(cell: UICollectionViewCell) {
    if let cell = cell as? AlarmItemCell {
        cell.greyView.hidden = true
    }
    print("2")
}

编辑2:

if let index = indexPath {
            let cell = AlarmCollection.cellForItemAtIndexPath(index)
            updateView(cell!)


        }

1 个答案:

答案 0 :(得分:0)

您应该能够使用从集合视图中获取的单元格来引用greyView。

例如,将您的updateView功能更改为:

func updateView(cell: UICollectionViewCell) {
    if let cell = cell as? AlarmItemCell {
      cell.greyView.hidden = true
    }
    print("2")
}

然后将您在此处的单元格var cell = AlarmCollection.cellForItemAtIndexPath(index)传递给该函数。 就你所知,你现在的方式并没有抓住正确的单元格。

编辑:

if let index = indexPath, 
   let cell = AlarmCollection.cellForItemAtIndexPath(index) {
    updateView(cell)
} else {
    print("Could not find index path or cell")
}