Ios UICollectionView中的UIImageView未正确绘制
我有一个应用程序,屏幕被分成四个相等的单元。每个单元格都有一个图像、标签和颜色。我正在尝试添加图像,但由于某种原因,只有第一个单元格中的图像可以工作 现在看起来是这样的: 这是我的密码: 在ViewController.swift中:Ios UICollectionView中的UIImageView未正确绘制,ios,swift,uiimageview,uicollectionview,Ios,Swift,Uiimageview,Uicollectionview,我有一个应用程序,屏幕被分成四个相等的单元。每个单元格都有一个图像、标签和颜色。我正在尝试添加图像,但由于某种原因,只有第一个单元格中的图像可以工作 现在看起来是这样的: 这是我的密码: 在ViewController.swift中: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
colorArray += [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]
pictureArray += [UIImage(named: "budget")!, UIImage(named: "journey")!,
UIImage(named: "learn")!, UIImage(named: "settings")!]
titleArray += ["Budget", "Journey", "Learn", "Settings"]
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell
// Set cell properties
cell.backgroundColor = colorArray[indexPath.row]
let imageview:UIImageView=UIImageView(image: pictureArray[indexPath.row])
let size = CGSize(width: 100, height: 100)
imageview.center = cell.center
imageview.bounds = CGRect(origin: cell.bounds.origin, size: size)
cell.contentView.addSubview(imageview)
let label = cell.viewWithTag(1) as! UILabel
label.text = titleArray[indexPath.row]
return cell
}
标签和颜色工作正常,但由于某些原因,图像视图似乎不在屏幕上。我有一种感觉,我的中心/边界限制正在迫使图像离开屏幕,但我尝试了许多组合,它们似乎都不适合我
我该怎么做呢?我在这里发布了一个正确的答案,供谁来谷歌搜索。(因为一小时前就解决了,没有人给出答案) 最佳做法是将
UICollectionViewCell
子类化,连接所有插座(标签、图像等),并在单元格上使用此类
祝大家好运我在这里发布了一个正确的答案,供大家在这里搜索。(因为一小时前就解决了,没有人给出答案) 最佳做法是将
UICollectionViewCell
子类化,连接所有插座(标签、图像等),并在单元格上使用此类
祝大家好运在约格什·萨塔尔的评论之后,这就是成功的原因: I子类UICollectionViewCell,连接图像和标签:
class NavigationCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var navImage: UIImageView!
@IBOutlet weak var navLabel: UILabel!
}
然后,在ViewController中,我执行了以下操作:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NavigationCollectionViewCell
// Set cell properties
cell.backgroundColor = colorArray[indexPath.row]
cell.navImage.image = imageArray[indexPath.row]
cell.navLabel.text = titleArray[indexPath.row]
return cell
}
在Main.storyboard中设置图像和标签的大小和位置 在Yogesh Suthar的评论之后,这就是成功的原因: I子类UICollectionViewCell,连接图像和标签:
class NavigationCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var navImage: UIImageView!
@IBOutlet weak var navLabel: UILabel!
}
然后,在ViewController中,我执行了以下操作:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NavigationCollectionViewCell
// Set cell properties
cell.backgroundColor = colorArray[indexPath.row]
cell.navImage.image = imageArray[indexPath.row]
cell.navLabel.text = titleArray[indexPath.row]
return cell
}
在Main.storyboard中设置图像和标签的大小和位置 将所需视图作为子类插入,并将所有约束设置为cell.heightAchor等。由于某种原因,这将修复该错误。如果将子视图作为子视图添加到单元中,而不是作为单元的属性添加到单元中,则可以使用该方法将所需视图作为子类插入,并将所有约束设置为cell.heightAchor等。由于某种原因,这将修复该错误。如果将子视图作为子视图添加到单元中,而不是作为单元的属性添加到单元中,则该方法有效如果使用原型单元,效果会更好。您尝试查看视图层次结构了吗?在pictureArray的开始处放置一些其他图像,并检查该图像是否也在第一个单元格中绘制?@Yogeshuthar我创建了自己的UICollectionViewCell子类,其中包含图像和标题的IBOutlet,它成功了!非常感谢。如果你使用原型电池会更好。您尝试查看视图层次结构了吗?在pictureArray的开始处放置一些其他图像,并检查该图像是否也在第一个单元格中绘制?@Yogeshuthar我创建了自己的UICollectionViewCell子类,其中包含图像和标题的IBOutlet,它成功了!非常感谢。