gpt4 book ai didi

ios - didSelectItemAtIndexPath 中的 cell.contentView.viewWithTag 与 cell.viewWithTag 的工作方式不同

转载 作者:行者123 更新时间:2023-11-30 13:59:01 25 4
gpt4 key购买 nike

我的UICollectionView有一个UILabel和一个UIImageView,我认为它们并且工作得很好,如下所示:

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("coffee_brand_cell", forIndexPath: indexPath) //as! AURCoffeeBrandCollectionViewCell
let imageView = cell.contentView.viewWithTag(1) as? UIImageView

imageView?.image = UIImage(named: ["coffee1","coffee2","coffee3","coffee3"][indexPath.row])
let name = cell.contentView.viewWithTag(2) as? UILabel
name?.text = coffeeName[indexPath.row] + String(indexPath.row)
name?.textColor = UIColor(red: 226.0/255.0, green: 170.0/255.0, blue: 119.0/255.0, alpha: 1.0)
name?.adjustsFontSizeToFitWidth = true
cell.backgroundColor = collectionView.backgroundColor
cell.layoutIfNeeded()
imageView?.layer.cornerRadius = (cell.frame.width - 40)/2.0
imageView?.layer.masksToBounds = true

return cell
}

但是我无法获取所选单元格的名称和图像,如下所示,

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
if let name = cell?.contentView.viewWithTag(2) {
//Can't get into this.
}
}
}

单元格的 contentView subview 返回 0。

cell!.contentView.subviews
0 elements

更新:

cell.contentView.viewWithTag(2) 适用于 cellForItemAtIndexPath,但不适用于 didSelectItemAtIndexPath。但是 cell.viewWithTag(2) 可以工作。

为什么 contentView 的工作方式不同?

更新

将这两行插入到 didSelectItemAtIndexPath

        print(" selected cell is \((cell?.viewWithTag(2) as! UILabel).text!)")
print(" selected cell is \((cell?.contentView.viewWithTag(2) as! UILabel).text!)")

输出:

selected cell is Kii Kenya0
fatal error: unexpectedly found nil while unwrapping an Optional value

项目可以在这里下载:Link

PS:我检查了 contentView 的 subview 的“已安装”。 enter image description here模拟器中的 CollectionView。 enter image description here

最佳答案

尝试使用这个

import UIKit

class CollectionUIViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate{

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 20
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("customCellIdentifier", forIndexPath: indexPath)
(cell.contentView.viewWithTag(1) as! UIImageView).image = UIImage(named: "Image")
(cell.contentView.viewWithTag(2) as! UILabel).text = "cell no \(indexPath.row)"
return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
let selectedCell = collectionView.cellForItemAtIndexPath(indexPath)
print(" selected cell is \((selectedCell?.contentView.viewWithTag(2) as! UILabel).text!)")

}



}

我通过使用 viewWithTag 访问所选单元格的标签文本(随附快照供引用) enter image description here

关于ios - didSelectItemAtIndexPath 中的 cell.contentView.viewWithTag 与 cell.viewWithTag 的工作方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33207155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com