Ios Swift-减少UICollectionViewCell之间的间隙
即使我在Ios Swift-减少UICollectionViewCell之间的间隙,ios,swift,autolayout,Ios,Swift,Autolayout,即使我在UICollectionViewCell检查器中实现了自动布局和一些设置,我的UICollectionViewCell也不好看 我的背景是这样的 我的UICollectionViewCell自动布局 我的ImageView自动布局 我的代码 PopularViewController.swift import UIKit import Alamofire class PopularViewController: UIViewController,UICollectionViewD
UICollectionViewCell
检查器中实现了自动布局和一些设置,我的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!)
}
}
}
我的输出
我知道现在回复很晚,但它可能对其他人有用 可以使用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
}
集合视图将在一行上容纳尽可能多的项,该行可以容纳给定的边插入和最小项间距。要使间隙更小,您需要使单元更大,或者使单元足够小,以便在一条线上多容纳一个单元。您的收藏视图是水平滚动还是垂直滚动?我的收藏视图是水平滚动。那我该怎么修呢?我说了怎么修。你需要把你的细胞变大。它们应该是(collectionView.frame.size.width-2*单元间间隙)/3。这假设你需要3个单元格。这对我有帮助。。谢谢。