2015-11-27 62 views
1

在我的CollectionView的細胞具有類MyCollectionViewCell編輯挖掘CollectionViewCell

class MyCollectionViewCell: UICollectionViewCell { 

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

}

我想設置標籤,無文本,並在細胞的ImageView的顯示圖像是被挖掘。 的collectionViewController貼有以下類UND代碼:

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 

} 

}

我怎樣才能修改抽頭單元,我把代碼中的註釋?

回答

1

在您的評論的地方,你可以使用:

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

你需要填寫映像名稱或選擇基於該圖像是來自不同的方法。

+0

謝謝!可悲的是它說:「找不到會員'隱藏'」和「找不到會員'圖片'」 – maidi

+0

@maidi嗯...嘗試添加我添加到我的答案的類型演員,並讓我知道它是否有效。 – Daniel

+0

是的,現在它工作了! – maidi