【问题标题】:Hide View in UICollectionViewCell在 UICollectionViewCell 中隐藏视图
【发布时间】:2016-05-12 15:00:26
【问题描述】:

谁能告诉我它不会在模拟器中隐藏我的 UIView? 该代码正在运行,它会打印我添加到函数中的数字,但它仍然不起作用。

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

 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!)


        }

【问题讨论】:

  • 你应该在发布之前清理你的代码。保留像 didReceiveMemoryWarning() 这样不必要的代码和像 //Do additional setup after loading... 这样不需要的 cmets 会使阅读变得更加困难,并且可能会带走愿意提供帮助的人。也许你可以编辑你的帖子?
  • 好的,谢谢。

标签: xcode swift uicollectionview cell


【解决方案1】:

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

例如,将您的 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")
}

【讨论】:

  • 您还应该尽可能避免强制转换和强制展开,以避免崩溃和意外错误。
  • 感谢您的帮助!但我还是有点卡住了你能告诉我我做错了什么吗? @ebearden 我更新了主要问题
  • 您的updateView 函数是否被调用,如果是,您是否将其放入隐藏视图的块中?
  • 根据您的编辑,我认为您想要的是: var cell = AlarmCollection.cellForItemAtIndexPath(index) updateView(cell)
  • 哇,非常感谢!但显然,如果不强制展开,这将不起作用。我会将工作代码放入编辑中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
相关资源
最近更新 更多