我在swift中创建了集合视图 语言我需要向这些单元格添加图像如何添加图像?
class ViewController: UIViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
var collectionView: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView!.backgroundColor = UIColor.blueColor()
self.view.addSubview(collectionView!)
// Do any additional setup after loading the view, typically from a nib.
}
答案 0 :(得分:0)
最简单的方法是在界面构建器中创建集合视图,子类uicollectionviewcell为uiimageview添加插座
class colCollectionViewCell: UICollectionViewCell {
@IBOutlet var myImageView: UIImageView!
}
在该collectionview中添加一个动态单元格,将该单元格的类更改为您的子类。删除该单元格上的UIImageView,连接您的插座并从UICollectionView委托/数据源函数访问它。
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as colCollectionViewCell
cell.myImageView.image = UIImage(named:"myimagename")
return cell
}
这是UICollectionView的一个很棒的教程:http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1
答案 1 :(得分:0)
你可以使用Haneke
样品
if let urlString = self.data?["images"]["standard_resolution"]["url"]{
let url = NSURL(string: urlString.stringValue)
self.imageView.hnk_setImageFromURL(url!)
}