Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios didSelectItemAtIndexPath中的cell.contentView.viewWithTag与cell.viewWithTag的工作原理不同_Ios_Swift_Uicollectionviewcell - Fatal编程技术网

Ios didSelectItemAtIndexPath中的cell.contentView.viewWithTag与cell.viewWithTag的工作原理不同

Ios didSelectItemAtIndexPath中的cell.contentView.viewWithTag与cell.viewWithTag的工作原理不同,ios,swift,uicollectionviewcell,Ios,Swift,Uicollectionviewcell,我的UICollectionView有一个UILabel和一个UIImageView,我对它们进行了分析,工作原理如下: func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ let cell = collectionView.dequeueReusableCellWithRe

我的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子视图返回0

cell!.contentView.subviews
0 elements
更新:

cell.contentView.viewWithTag2适用于cellForItemAtIndexPath,但不适用于didSelectItemAtIndexPath。但是cell.viewWithTag2可以工作

为什么contentViewworks不同

更新

将这两行插入didSelectItemAtIndexPath

输出:

selected cell is Kii Kenya0
fatal error: unexpectedly found nil while unwrapping an Optional value
项目可在此处下载:

PS:我检查了contentView子视图的“已安装”。 模拟器中的CollectionView。

尝试使用此

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-attached snapshot访问选定单元格的标签文本以供参考,从而获取该单元格的标签文本

cell.subview还是cell?.viewWithTagWilliam,您知道如何创建UICollectionViewCell的子类吗?这会比通过标记访问视图提供更多的控制。丹尼尔,你知道你必须为两个连接将其子类化吗?@hossamghareb它确实是有线的,我使用cell.contentView.viewWithTag2在cellForItemAtIndexPath上工作,但只有cell?.viewWithTag在didSelectItemAtIndexPath中工作,我猜contentView可能不在视图层次结构中。创建一个子类,并通过它而不是标签访问标签和图像。这也会更容易打印所选单元格为\cell?。查看标记2为!UILabel.text!打印所选单元格为\cell?.contentView.viewWithTag2 as!UILabel.text!输出:>所选单元格为rowText>致命错误:在展开一个可选值时意外发现nil这很有趣,为什么它不在我的上,也更新了我的问题。是的,这很奇怪,因为它在我的Xcode上使用cell?.viewWithTag和cell.contentView.viewWithTag工作正常。我们的区别是collectionView的superView不是viewcontroler的视图,而是view controller的子视图,但这不应该是问题所在。在点击任何单元格之前,您能够看到具有适当单元格的集合视图吗?
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!)")

    }



}