Swift 3 iOS 10-UICollectionView,图像和文本不';我不能正常出现
我是一名iOS开发新手,我一直致力于实现UICollectionView。下面是它的表现: 以下是我在故事板中的设计: 我正试图根据本教程实现这一点: 以下是我的ViewController类:Swift 3 iOS 10-UICollectionView,图像和文本不';我不能正常出现,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我是一名iOS开发新手,我一直致力于实现UICollectionView。下面是它的表现: 以下是我在故事板中的设计: 我正试图根据本教程实现这一点: 以下是我的ViewController类: class SelectMorphologyViewController: UIViewController { @IBOutlet weak var collectionView: UICollectionView! var morphology = Morphology
class SelectMorphologyViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var morphology = Morphology.getMorphology()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
struct Storyboard {
static let cellIdentifier = "Morphology cell"
}
}
extension SelectMorphologyViewController : UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return morphology.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.cellIdentifier, for: indexPath) as! MorphologyCollectionViewCell
cell.morphology = self.morphology[indexPath.item]
return cell
}
}
模型类:
class Morphology
{
var title = ""
var image : UIImage!
init(title : String , image : UIImage) {
self.title = title
self.image = image
}
static func getMorphology() -> [Morphology] {
return [Morphology(title : "Les épaules qui s’alignent avec les hanches." , image : UIImage (named: "8")!), Morphology(title : "Les épaules et des hanches dans le même alignement et une taille très peu marquée. " , image : UIImage (named: "h")!),Morphology(title : "Une petite poitrine, une taille fine et des épaules sont dans l’alignement des hanches." , image : UIImage (named: "x")!),Morphology(title : "Une carrure est plus large que les hanches." , image : UIImage (named: "v")!),Morphology(title : "Une carrure plus étroite que les hanches. " , image : UIImage (named: "a")!),Morphology(title : "La femme ronde :) " , image : UIImage (named: "o")!)]
}
}
class MorphologyCollectionViewCell: UICollectionViewCell {
var morphology : Morphology! {
didSet {
updateUI()
}
}
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var title: UILabel!
private func updateUI() {
image?.image! = morphology.image
title?.text! = morphology.title
}
}
最后是我的CollectionViewCell类:
class Morphology
{
var title = ""
var image : UIImage!
init(title : String , image : UIImage) {
self.title = title
self.image = image
}
static func getMorphology() -> [Morphology] {
return [Morphology(title : "Les épaules qui s’alignent avec les hanches." , image : UIImage (named: "8")!), Morphology(title : "Les épaules et des hanches dans le même alignement et une taille très peu marquée. " , image : UIImage (named: "h")!),Morphology(title : "Une petite poitrine, une taille fine et des épaules sont dans l’alignement des hanches." , image : UIImage (named: "x")!),Morphology(title : "Une carrure est plus large que les hanches." , image : UIImage (named: "v")!),Morphology(title : "Une carrure plus étroite que les hanches. " , image : UIImage (named: "a")!),Morphology(title : "La femme ronde :) " , image : UIImage (named: "o")!)]
}
}
class MorphologyCollectionViewCell: UICollectionViewCell {
var morphology : Morphology! {
didSet {
updateUI()
}
}
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var title: UILabel!
private func updateUI() {
image?.image! = morphology.image
title?.text! = morphology.title
}
}
以下是我的故事板设置的一些图像:
您应该尝试以下方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.width, height: self.view.frame.height)
}
您应该尝试以下方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.width, height: self.view.frame.height)
}
如果您发布了一些代码,我的回答会更准确
问题似乎出在UICollectionViewCell大小调整上。故事板不会反映细胞的呈现方式,它对你来说更像是一种视觉效果。为了修复它,您可能需要实现:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
如果您发布了一些代码,我的回答会更准确
问题似乎出在UICollectionViewCell大小调整上。故事板不会反映细胞的呈现方式,它对你来说更像是一种视觉效果。为了修复它,您可能需要实现:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
试试这个:
添加此功能并根据需要进行更改
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: CGFloat(collectionView.frame.size.width / 2 - 5), height: CGFloat(135)) // 2 is for number of cell in one row you can change as per your needs
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets{
return UIEdgeInsetsMake(0, 0, 0, 0)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
试试这个:
添加此功能并根据需要进行更改
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: CGFloat(collectionView.frame.size.width / 2 - 5), height: CGFloat(135)) // 2 is for number of cell in one row you can change as per your needs
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets{
return UIEdgeInsetsMake(0, 0, 0, 0)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
添加一些代码您正在执行的操作,是否可以为collectionView委托和数据源添加代码,以及与集合视图布局相关的情节提要设置?是否为collectionView添加了布局约束添加了一些代码您正在执行的操作,是否可以为collectionView委托和数据源添加代码,以及与集合视图布局相关的情节提要设置?是否在集合视图上添加了布局约束?我刚刚添加了此函数,没有任何更改。我刚刚添加了此函数,没有任何更改