嘿,我有一些执行正常的代码,但我无法从UICollectionView中取消选择一个单元格。一旦被选中,它就会保持选中状态。尝试了其他帖子中的大部分方法,但无法解决。请看看,让我知道
MY 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
}
}
究竟是什么错误?我无法取消选择细胞?因为我在选定的....上设置了不同的颜色。
答案 0 :(得分:0)
您可以简单地创建一个功能,使用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
}