Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 swift集合视图_Ios_Swift - Fatal编程技术网

iOS swift集合视图

iOS swift集合视图,ios,swift,Ios,Swift,我已经在swift中创建了集合视图 语言我需要向这些单元格中添加图像如何添加图像 class ViewController: UIViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource { var collectionView: UICollectionView? override func viewDidLoad() { super.viewDidLoad()

我已经在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.
    }

最简单的方法是在interface builder的子类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的精彩教程:

您可以使用Haneke

样品

if let urlString = self.data?["images"]["standard_resolution"]["url"]{
            let url = NSURL(string: urlString.stringValue)
            self.imageView.hnk_setImageFromURL(url!)
        }
参考: