之间的差距即使我在UICollectionViewCell
检查器中实现自动布局和一些设置,我的UICollectionViewCell
也不好看。Swift - 缩小UICollectionViewCell
我的设置是这样的
我UICollectionViewCell自动版式
我的ImageView自动版式
我的代码
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!)
}
}
}
我的输出
请指点。谢谢。
集合视图将适合在一行尽可能多的项目,能适合给边缘插图和最小项目间的间距。为了使间隙更小,您需要使细胞变大,或者更小,以使另一个细胞能够适应一条线。您是收集视图水平还是垂直滚动? – rdelmar
我的收藏视图是水平滚动。所以我如何解决它? –
我说过如何解决它。你需要让你的细胞更大。它们应该是(collectionView.frame.size.width - 2 *单元格间隙)/ 3。这假定你想要3个单元。 – rdelmar