我正在尝试制作一个 Collection View ,其中每个单元格都包含一个图像和一个可以选择或不选择的按钮。最初一切似乎都运行良好,但是在我按下一些按钮并在 View 中移动后,一些“选定”按钮变为“未选定”,反之亦然。
我发现人们在图像切换位置时遇到问题,所以我尝试缓存我的图像。我也尝试过完全删除图像,但问题仍然存在。
这是我的收藏 View 设置:
override func collectionView(_: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
//cell.photo.image = imageArray[indexPath.row]
cell.selectButton.tag = indexPath.row
cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let length = collectionView.frame.width / 3 - 1
return CGSize(width: length, height: length)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
//sets spacing between images
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
//sets spacing between images
return 1.0
}
这是我的 buttonPressed 函数:
func buttonPressed(sender: UIButton) {
if sender.isSelected == true{
sender.setTitleColor(UIColor.blue, for:UIControlState.normal)
sender.isSelected = false
}else{
sender.setTitleColor(UIColor.red, for:UIControlState.normal)
sender.isSelected = true
}
}
如何解决?
最佳答案
您需要做的就是在 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
获取一个数组并将所有选定的索引保存在其中。
var mArray = [Int]()
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
//cell.photo.image = imageArray[indexPath.row]
cell.selectButton.tag = indexPath.row
cell.selectButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
//check if indexpath.row is selected
if mArray.contains(indexPath.row){
cell.selectButton.setTitleColor(UIColor.red, for:UIControlState.normal)
cell.selectButton.isSelected = true
}else{
cell.selectButton.setTitleColor(UIColor.blue, for:UIControlState.normal)
cell.selectButton.isSelected = false
}
return cell
}
func buttonPressed(sender: UIButton) {
if sender.isSelected == true{
mArray.removeAtIndex(sender.tag)
sender.setTitleColor(UIColor.blue, for:UIControlState.normal)
sender.isSelected = false
}else{
mArray.append = sender.tag
sender.setTitleColor(UIColor.red, for:UIControlState.normal)
sender.isSelected = true
}
}
关于ios - 滚动时Swift 3选择的按钮在 Collection View 中改变位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43019264/