gpt4 book ai didi

swift - 使用 didSelect 方法在 viewDidLoad 后在 Cell 中显示图像

转载 作者:行者123 更新时间:2023-11-30 10:36:18 25 4
gpt4 key购买 nike

我有一个单元格,其中包含一个空的 UIImageView 和一个嵌套在包含一行图像的 collectionView 中。我创建了一个 didSelect 函数,我想突出显示所选行并在空 UIImageView 中显示它的图像。

我尝试了 reloadData 函数,但应用程序崩溃了。我在没有 reloadData 函数的情况下尝试了它,并且索引路径的项目仅突出显示。

class DisplayImageCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

private let displayCell = "DisplayImageCell"

let imageContainer: UIImageView = {
let imageContainer = UIImageView()
imageContainer.clipsToBounds = true
imageContainer.backgroundColor = UIColor(patternImage: (UIImage(imageLiteralResourceName: "ic_person_outline_white_2x").withRenderingMode(.alwaysTemplate)))
imageContainer.isUserInteractionEnabled = true
imageContainer.translatesAutoresizingMaskIntoConstraints = false
return imageContainer
}()

let pictureCollection: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 5
layout.minimumInteritemSpacing = 10
let imageContainer = UICollectionView(frame: .zero, collectionViewLayout: layout)
imageContainer.showsHorizontalScrollIndicator = false
imageContainer.clipsToBounds = true
imageContainer.tintColor = .yellow
imageContainer.isUserInteractionEnabled = true
imageContainer.translatesAutoresizingMaskIntoConstraints = false
return imageContainer
}()

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear

pictureCollection.dataSource = self
pictureCollection.delegate = self
pictureCollection.register(ImageCell.self, forCellWithReuseIdentifier: displayCell)

addSubview(imageContainer)
NSLayoutConstraint.activate([
imageContainer.topAnchor.constraint(equalTo: topAnchor),
imageContainer.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.80),
imageContainer.widthAnchor.constraint(equalTo: widthAnchor)
])

addSubview(pictureCollection)
NSLayoutConstraint.activate([
pictureCollection.topAnchor.constraint(equalTo: imageContainer.bottomAnchor, constant: 10),
pictureCollection.bottomAnchor.constraint(equalTo: bottomAnchor),
pictureCollection.widthAnchor.constraint(equalTo: widthAnchor)
])

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 7
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: displayCell, for: indexPath) as! ImageCell
let imageOption = ImageOption(rawValue: indexPath.row)
cell.iconImageView.image = imageOption?.icon()
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: pictureCollection.frame.height, height: pictureCollection.frame.height)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: true)
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: false)
}

func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: true)
}

func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
updateCell(having: indexPath, selected: false)
}

private class ImageCell: UICollectionViewCell {

let iconImageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFit
iv.clipsToBounds = true
iv.translatesAutoresizingMaskIntoConstraints = false
iv.isUserInteractionEnabled = true
return iv
}()

override init(frame: CGRect) {
super.init(frame: .zero)

backgroundColor = .blue

addSubview(iconImageView)
NSLayoutConstraint.activate([
iconImageView.heightAnchor.constraint(equalTo: heightAnchor),
iconImageView.widthAnchor.constraint(equalTo: widthAnchor),
iconImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
iconImageView.centerYAnchor.constraint(equalTo: centerYAnchor)
])

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func updateCell(having indexPath: IndexPath, selected: Bool) {

let selectedBackgroundColor = UIColor(red: 41/255.0, green: 211/255.0, blue: 241/255.0, alpha: 1.0)
let defaultBackgroundColor = UIColor.blue
let imageSlot = DisplayImageCell()
let ep = EditProfileController()

if let cell = pictureCollection.cellForItem(at: indexPath) {
cell.contentView.backgroundColor = selected ? selectedBackgroundColor : defaultBackgroundColor
imageSlot.imageContainer.image = UIImage(named: "ThumbsUp")
ep.collectionView.reloadData()
}
}
}

预期的结果是空 ImageView 显示所选indexPath.row的图像。当我尝试重新加载 collectionView 时,发现一个 nil 值的 fatal error 。

最佳答案

如果您想在 imageContainer 中显示 Collection View 的数据源中选定的图像,您只需在 didSelectItemAt indexPath 中获取图像,就像在cellForItemAt indexPath:

let imageOption = ImageOption(rawValue: indexPath.item)

然后

imageContainer.image = imageOption?.icon()

实际上没有必要调用reloadData(),因为 Collection View 的数据源尚未更新。

关于swift - 使用 didSelect 方法在 viewDidLoad 后在 Cell 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57930744/

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