我是编码新手,所以请原谅我的乱码。我试图在点击按钮时更改按钮的图像。该按钮位于UICollectionViewCell中,这有点棘手。
这是将按钮添加到单元格的位置:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let d = self.data[indexPath.row].image
let button = UIButton(frame: CGRect(x:0, y:0, width:68,height:80))
button.setImage(d, for: UIControl.State.normal)
button.addTarget(self, action: #selector(buttonTapped), for: UIControl.Event.touchUpInside)
cell.addSubview(button)
return cell }
这是按钮从中获取其图像的数据数组:
var data: [CustomData] = [CustomData]()
override func viewDidLoad() {
super.viewDidLoad()
self.data.append(CustomData(title: "abc", image: #imageLiteral(resourceName: "bookOne"), url: "typeURLa"))
self.data.append(CustomData(title: "def", image: #imageLiteral(resourceName: "bookTwo"), url: "typeURLb"))
self.data.append(CustomData(title: "ghi", image: #imageLiteral(resourceName: "bookThree"), url: "typeURLc"))
}
点击按钮时会发生以下情况:
@objc func buttonTapped(sender: UIButton!) {
print("button tapped")
let r = data[0]
print(sender.currentImage!)
print(r.image)
sender.setImage(r.image, for: UIControl.State.normal)
collectionView.reloadData()
print(sender.currentImage!)
print(r.image)
}
在调试控制台中,我可以看到它确实更新了按钮图像,但在模拟器中,图像保持不变。
button tapped
<UIImage:0x600000adad90 named(main: bookTwo) {600, 754}>
<UIImage:0x600000adafd0 named(main: bookOne) {798, 1210}>
<UIImage:0x600000adafd0 named(main: bookOne) {798, 1210}>
<UIImage:0x600000adafd0 named(main: bookOne) {798, 1210}>
当我点击第2行中保存图像“bookTwo”的按钮时,我希望它更改为图像“bookOne”。轻按按钮后,在第2行中打印按钮的图像时,我可以看到它在调试控制台中成功将“bookTwo”更改为“bookOne”图像。位于数组data [0]中的“bookOne”和位于data [1]中的“bookTwo”。但是在屏幕上仍然显示“bookTwo”图像。
最佳答案
问题是您在每次更新单元后都一直在重新加载collectionView。每当您重新加载collectionView时,都会调用collectionViewCellForItemAt方法。但是在collectionViewCellForItemAt中,方法已设置为将按钮中的图像写为数据数组的indexpath.row中的图像。由于数据数组未更改,因此将单元格重新设置为数据数组给定的默认图像。
相反,您可以更新代码。更新后的代码如下所示
@objc func buttonTapped(sender: UIButton!) {
print("button tapped")
let row = sender.tag
let intialData = data[row]
print(sender.currentImage!)
print(data[row].image)
data[row] = CustomData(title: intialData.title, image: #imageLiteral(resourceName: "bookOne"), url: intialData.url)
sender.setImage(data[row].image, for: UIControl.State.normal)
print(sender.currentImage!)
print(data[row].image)
}
在collectionViewCellForItemAt中添加此行button.tag = indexPath.row。更新后的代码如下所示
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let d = self.data[indexPath.row].image
let button = UIButton(frame: CGRect(x:0, y:0, width:68,height:80))
button.setImage(d, for: UIControl.State.normal)
button.tag = indexPath.row
button.addTarget(self, action: #selector(buttonTapped), for: UIControl.Event.touchUpInside)
cell.addSubview(button)
return cell
}
关于ios - Swift:UICollectionViewCell中的UIButton图像不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62338655/