编辑点击的 CollectionViewCell
Edit tapped CollectionViewCell
在我的 CollectionView 中,单元格具有 class MyCollectionViewCell
:
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var MyImageView: UIImageView!
}
我想将标签的文本设置为空,并在点击的单元格的 imageView 中显示图像。
collectionViewController 具有以下 class 和附加代码:
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:"...")
您需要填写图片名称或根据图片来源选择不同的方法。
在我的 CollectionView 中,单元格具有 class MyCollectionViewCell
:
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var MyImageView: UIImageView!
}
我想将标签的文本设置为空,并在点击的单元格的 imageView 中显示图像。 collectionViewController 具有以下 class 和附加代码:
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:"...")
您需要填写图片名称或根据图片来源选择不同的方法。