UICollectionView adding image to a cell

后端 未结 2 1854
自闭症患者
自闭症患者 2020-11-28 12:28

I\'m working on a project which uses UICollectionView to display data which may or may not have an image.

The way I\'m using is to check if the image ur

2条回答
  •  悲哀的现实
    2020-11-28 12:42

    Code for Swift 4

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {   
        return 1   
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    
        var imageview:UIImageView=UIImageView(frame: CGRect(x: 50, y: 50, width: 200, height: 200));
    
            var img : UIImage = UIImage(named:"Your image name")
            imageview.image = img
    
            cell.contentView.addSubview(imageview)
    
        }
    
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 50, height: 414)
    }
    

提交回复
热议问题