我有一个UICollectionView
该自定义包含UICollectionViewCell
的,这里是有关的代码: UICollectionView定制单元不显示
class customCell: UICollectionViewCell{
var imgView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit(){
imgView.translatesAutoresizingMaskIntoConstraints = false
self.addcontentView.addSubview(imgView)
//add constraints
}
}
class CollectionViewController: UICollectionViewController {
private let imageLib = [...] //image file name strings
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : IconCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(keyIconCellReuse , forIndexPath: indexPath) as! IconCollectionViewCell
let image1 = (UIImage(named: imageLib[indexPath.row])?.imageWithRenderingMode(.AlwaysTemplate))!
cell.imgView = UIImageView(image: image1)
cell.imgView.tintColor = UIColor(white:0, alpha:0.7)
cell.userInteractionEnabled = true
return cell
}
}
这是它看起来像现在:
它似乎为每个加载的图像添加新的UIImageView。你能分享func setUp(imgString:String)方法体吗? –