ios - 编辑点击的 CollectionViewCell

标签 ios swift uicollectionviewcell collectionview

在我的 CollectionView 中,单元格具有 MyCollectionViewCell 类:

class MyCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var MyImageView: UIImageView!

我想将标签的文本设置为空,并在被点击的单元格的 imageView 中显示图像。 collectionViewController 附有以下类和代码:

class CollectionViewController: UICollectionViewController {

var array:[String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    array = ["1", "2", "3"]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return array.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell:MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! MyCollectionViewCell

    var Label = cell.viewWithTag(1) as! UILabel

    Label.text = array[indexPath.row]

    return cell
}

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    println("user tapped on cell number \(indexPath.row)")

    //change ImageView of tapped cell   

}

如何编辑我在代码中放置注释的点击单元格?

最佳答案

在你的评论处,你可以使用:

let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell
cell.myLabel.hidden = true
cell.MyImageView.image = UIImage(named:"...")

您需要填写图片名称或根据图片来源选择不同的方法。

关于ios - 编辑点击的 CollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966075/

相关文章:

objective-c - 避免 UIView 触摸延迟

ios - 在 iOS 应用程序 (Swift) 中使用 telegram 分享消息

ios - swift 和 Xcode : How to have a default list of countries?

ios - .reloadSections 后 UITableView 的偏移量不正确

ios - 将数组中的图像嵌入到 Collection View 的单元格中

ios - UITableView "cellForRowAt: indexPath"偶尔在核心数据属性上调用 "init?(coder aDecoder: NSCoder)"

ios - 如何从 Flutter 代码在 Swift 中打开相机 View 以访问自动对焦等相机功能

ios - ViewController 异常改变大小

ios - UICollectionView iOS8 的问题

ios - 在水平滚动中设置具有不同宽度单元格的 collectionView 的大小