gpt4 book ai didi

swift - UICollectionView 和取消选择单元格不起作用。一旦被选择。这里有什么错误?

转载 作者:行者123 更新时间:2023-11-30 13:14:51 25 4
gpt4 key购买 nike

嘿,我有一些代码可以正常执行,但我无法从 UICollectionView 中取消选择单元格。一旦被选择,它就会保持选中状态。尝试了其他帖子中的大部分方法,但无法解决。请看一下并告诉我

我的 VIEWCONTROLLER.SWIFT 部分

 @IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var selectedImg: UIImageView!
@IBOutlet weak var textLabela: UILabel!

var selectedPerson: Person?
var hasSelectedImage: Bool = false
let imagePicker = UIImagePickerController()



let missingPeople = [
Person(personImageUrl: "person1.jpg"),
Person(personImageUrl: "person2.jpg"),
Person(personImageUrl: "person3.jpg"),
Person(personImageUrl: "person4.jpg"),
Person(personImageUrl: "person5.jpg"),
Person(personImageUrl: "person6.png")
]

override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
imagePicker.delegate = self

let tap = UITapGestureRecognizer(target: self, action: Selector("loadPicker:"))
tap.numberOfTapsRequired = 1
selectedImg.addGestureRecognizer(tap)
collectionView.allowsMultipleSelection = true;
}





func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return missingPeople.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// so kur da napisam ovde

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PersonCell", forIndexPath: indexPath) as! PersonCell

//cell.selected = true
//collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: .None)

let person = missingPeople[indexPath.row]
cell.configureCell(person)
return cell
}





func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

self.selectedPerson = missingPeople[indexPath.row]
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! PersonCell
cell.configureCell(selectedPerson!)
cell.setSelected()

}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImg.image = pickedImage
hasSelectedImage = true
}
dismissViewControllerAnimated(true, completion: nil)
}

func showErrorAlert() {
let alert = UIAlertController(title: "Select Person & Image", message: "Please select a missing person to check and an image", preferredStyle: UIAlertControllerStyle.Alert)

let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil)
alert.addAction(ok)
self.presentViewController(alert, animated: true, completion: nil)
}

func loadPicker(gesture: UITapGestureRecognizer) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary // .Camera

presentViewController(imagePicker, animated: true, completion: nil)
} ..... ..... ...... and so on

PersonCell.swift

import UIKit
class PersonCell: UICollectionViewCell {

@IBOutlet weak var personImage: UIImageView!
var person: Person!



func configureCell(person: Person) {
self.person = person
self.addOrRemoveBorder()
if let url = NSURL(string: "\(baseURL)\(person.personImageUrl!)") {
downloadImage(url)


}
}

func downloadImage(url: NSURL) {
getDataFromUrl(url) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
self.personImage.image = UIImage(data: data)
self.person.personImage = self.personImage.image

}
}

}

func getDataFromUrl(url: NSURL, completion: ((data: NSData?, response: NSURLResponse?, error:NSError?) ->Void)){

NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
completion(data: data, response: response, error: error)
}.resume()

}

func addOrRemoveBorder() {
if self.person.isSelected == true {
personImage.clipsToBounds = false
personImage.layer.borderWidth = 2.0
personImage.layer.borderColor = UIColor.yellowColor().CGColor

} else {
personImage.layer.borderColor = UIColor.whiteColor().CGColor
personImage.layer.borderWidth = 0.0

}
}

func setSelected() {
self.person.isSelected = true
self.addOrRemoveBorder()
self.person.downloadFaceId()
super.selected=true

}
}

到底是什么错误?我无法取消选择该单元格?因为我在所选内容上设置了不同的颜色....

最佳答案

您可以简单地创建一个函数来取消选择单元格,就像使用 setSelected

func deselectCell() {
self.person.isSelected = false
self.addOrRemoveBorder()
//self.person.downloadFaceId() //remove face id or something here
super.selected = false
}
}

选项 2(更加动态)

  func selectedOrDeselectCell(isSelected: Bool) {
self.person.isSelected = isSelected
self.addOrRemoveBorder()
if isSelected{
self.person.downloadFaceId()
}else{
//self.person.downloadFaceId() //remove face id or something here
}
super.selected = isSelected
}

关于swift - UICollectionView 和取消选择单元格不起作用。一旦被选择。这里有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38338812/

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