2015-06-03 56 views
0

之间的差距即使我在UICollectionViewCell检查器中实现自动布局和一些设置,我的UICollectionViewCell也不好看。Swift - 缩小UICollectionViewCell

我的设置是这样的

enter image description here

我UICollectionViewCell自动版式

enter image description here

我的ImageView自动版式

enter image description here

我的代码

PopularViewController.swift

import UIKit 
import Alamofire 

class PopularViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

    var users: [JSON] = [] 
    @IBOutlet var collectionView: UICollectionView! 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     //#warning Incomplete method implementation -- Return the number of items in the section 
     return users.count 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserCell", forIndexPath: indexPath) as! PopularCollectionViewCell 

     cell.user = self.users[indexPath.row] 
     return cell 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     Alamofire.request(.GET, "http://128.199.160.213/datetick/users.json").responseJSON { (request, response, json, error) in 

      if json != nil { 
       var jsonObj = JSON(json!) 
       if let data = jsonObj["users"].arrayValue as [JSON]?{ 
        self.users = data 
        self.collectionView.reloadData() 
       } 
      } 

     } 
    } 
} 

PopularCollectionViewCell.swift

class PopularCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var profilePicture:UIImageView! 

    var user:JSON?{ 
     didSet{ 
      self.setupUser() 
     } 
    } 

    func setupUser(){ 

     if let urlString = self.user?["profilePhoto"]{ 
      let url = NSURL(string: urlString.stringValue) 
      self.profilePicture.hnk_setImageFromURL(url!) 
     } 

    } 
} 

我的输出

enter image description here

请指点。谢谢。

+0

集合视图将适合在一行尽可能多的项目,能适合给边缘插图和最小项目间的间距。为了使间隙更小,您需要使细胞变大,或者更小,以使另一个细胞能够适应一条线。您是收集视图水平还是垂直滚动? – rdelmar

+0

我的收藏视图是水平滚动。所以我如何解决它? –

+0

我说过如何解决它。你需要让你的细胞更大。它们应该是(collectionView.frame.size.width - 2 *单元格间隙)/ 3。这假定你想要3个单元。 – rdelmar

回答

0

我知道它迟到了,但它可能对其他人有用。

您可以使用UICollectionViewDelegateFlowLayout来减小尺寸。您已经继承了这个类在你PopularViewController

使用下面的函数来做到这一点

//To Show 3 items per row 
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSize(width: (collectionView.frame.size.width - 3)/3, height: 110) 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
    return 1 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
    return 1 
} 
+0

这帮了我..非常感谢。 – Badrinath

相关问题