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)
}
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