UICollectionViewController无法到达单元格

时间:2017-05-22 23:38:29

标签: ios xcode swift3 uicollectionview uicollectionviewcell

我试图在UICollectionViewCell中更改标签的文本。 ViewCell类链接到单元格,插座设置,并在viewController中注册

// Register cell classes
    self.collectionView!.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

并在cellForItemAt ...

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCollectionViewCell

但我无法访问该标签。与imageView相同的问题。如果我尝试使用以下方式设置文本:

cell.hund.text = "bla"

标签" hund"已知,但它会引发标签为零。

谢谢汤姆

ImageCollectionVC

import UIKit
private let reuseIdentifier = "cell"

class ImageCollectionVC: UICollectionViewController {

var listOfImages = [Int: KKImage]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    self.collectionView!.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    listOfImages = xmlParseImageSource()
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCollectionViewCell

    let imgUrl = listOfImages[indexPath.row]?.imagePreviewUrl

    do {
        let bindData = try Data(contentsOf: imgUrl!)
        let img = UIImage(data: bindData)

        cell.hund.text = "bla"

        //cell.img.image = img
        //cell.img.image = UIImage(data: bindData)
    } catch {
        print(error.localizedDescription)
    }
    cell.backgroundColor = UIColor.blue
    return cell
}
}

ImageCollectionViewCell

import UIKit

class ImageCollectionViewCell: UICollectionViewCell {
 @IBOutlet weak var img: UIImageView!
 @IBOutlet weak var hund: UILabel!  
}

1 个答案:

答案 0 :(得分:0)

Thomas从nib中醒来只是初始化你的UIImageView和UILabel并将它们添加到单元格中,它应该可以工作:)

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

如果您使用的是xib,请不要注册该类,而应使用nib注册:

 override func awakeFromNib() {
   super.awakeFromNib()
   // Initialization code, you obviously need to set the frames/constraints as per your requirement

   img = UIImageView(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
   hund = UILabel(frame: CGRect(x: 50, y: 0, width: 300, height: 40))
   self.addSubview(img)
   self.addSubview(hund)
}