gpt4 book ai didi

ios - Swift - 减少 UICollectionViewCell 之间的间隙

转载 作者:行者123 更新时间:2023-11-30 14:19:43 25 4
gpt4 key购买 nike

即使我在 UICollectionViewCell 检查器中实现自动布局和一些设置,我的 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

最佳答案

我知道回复晚了,但可能对其他人有用。

您可以使用 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
}

关于ios - Swift - 减少 UICollectionViewCell 之间的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30626086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com