2016-07-12 15 views
0

私はうまく実行するいくつかのコードがありますが、UICollectionViewからセルを選択解除できません。一度選択されると選択状態を維持します。他の投稿のほとんどのメソッドを試しましたが、解決できませんでした。見てください、私は正確にエラーのようです何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

あなたは単にあなたがsetSelected

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

オプション2(よりダイナミックなビット)

を持っているように、セルを選択解除機能を作成することができます....イムは異なる色を設定する原因となります多くの人が^ _^,,,,これは本当に助け、私は私のビューコントローラに didDeselectItemAtIndexPathを作成し、あなたのメソッドを呼び出した
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 
    } 
+0

感謝。出来た!時計仕掛けのような!ありがとうございました... –

関連する問題