2016-09-02 5 views
0

Ich mache gerade eine Anwendung, die den Benutzer erfordert, Bild-Picker zu verwenden, um Bilder auf CollectionViewCell zu laden. Ich benutze eine Prototyp-Zelle, die die UIImageView hat, aber wenn ich ein Foto poste, geht es nur zur ersten Zelle und nicht zu den einzelnen Zellen, die ich gedrückt habe. Ich habe mich gefragt, wie ich den Code so erstellen könnte, dass er auf die bestimmte Zelle abzielt, die ich angetippt habe, und den Bild-Picker entsprechend verwende.So verwenden Sie die Bildauswahl für die Collection View-Zellen

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.items.count 
} 
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    // get a reference to our storyboard cell 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell 

    cell.myLabel.text = self.items[indexPath.item] 
    cell.tag = self.integer[indexPath.item] 

    cell.backgroundColor = UIColor.yellowColor() 

    cell.myAdd.addTarget(self, action: "importPicture:", forControlEvents: UIControlEvents.TouchUpInside) 


    return cell 
} 

func importPicture(sender: UIButton) { 
    imagePicker.delegate = self 
    imagePicker.sourceType = .PhotoLibrary 
    imagePicker.allowsEditing = true 
    presentViewController(imagePicker, animated: true, completion: nil) 
} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    let 
    cell = myCollection.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! MyCollectionViewCell 
    var newImage: UIImage 

    imagePicker.dismissViewControllerAnimated(true, completion: nil) 
    let pickedImage1 = info[UIImagePickerControllerEditedImage] as? UIImage 
     currentPickerTarget.image = pickedImage1 
     currentPickerTarget = cell.myImages 


    myCollection.reloadData() 

} 

// MARK: - UICollectionViewDelegate protocol 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    // handle tap events 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell 
    print("You selected cell #\(indexPath.item)!") 
    print("\(indexPath.row)") 
    cell.backgroundColor = UIColor.blueColor() 
} 

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    // 1 
    // Return the number of sections 
    return 1 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let longPressGesture = UILongPressGestureRecognizer(target: self, action: "handleLongGesture:") 
    self.myCollection.addGestureRecognizer(longPressGesture) 
} 
+0

Wann aktualisierst du currentPickerTarget? Allgemeine Idee: Speichern Sie den ausgewählten IndexPath in einer Variablen in didSelectItemAtIndexPath und laden Sie das Bild nach dem Aufnehmen des Bildes mit dem richtigen Bild neu. – firstinq

Antwort

0

dies geschieht, weil Sie das aufgenommene Bild neu schreiben und sie dem Index 0 des Arrays hinzugefügt, würde ich die Linie cell.myAdd.addTarget(self, action: "importPicture:", forControlEvents: UIControlEvents.TouchUpInside) entfernen und nur die Funktion func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { } modifizieren, um die indexpath.row der Zelle speichern Sie wählten früh, dann führe die Funktion importPicture aus und wenn der Benutzer fertig ist, sein Bild zu speichern, speicherst du es in deinem Bilder-Array mit dem zuvor gespeicherten Index. Wenn du die selbe Zelle wählst, wird das Bild sich selbst neu schreiben und neue Zellen hinzufügen Bilder zum Array.

Verwandte Themen