0

I am currently building an application with the goal that the user can upload an image to a desired Collection View Cell by tapping on the button within a cell. So far I have been able to pick an image, but it will never go to the designated cell. For the ease of the question, I'll show the code I have used for the first three cells.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let returnCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! FirstCollectionViewCell

    if indexPath.row == 0 {
        let cell1 = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! FirstCollectionViewCell
        cell1.backgroundColor = UIColor.redColor()
        cell1.firstAdd.addTarget(self, action: "importPicture:", forControlEvents: UIControlEvents.TouchUpInside)
        currentPickerTarget = cell1.firstImages
        return cell1

    } else if indexPath.row == 1 {

        let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! SecondCollectionViewCell
        cell2.backgroundColor = UIColor.blueColor()
        cell2.secondAdd.addTarget(self, action: "secondImport:", forControlEvents: UIControlEvents.TouchUpInside)
        secondPickerTarget = cell2.secondImages
        return cell2

    } else if indexPath.row == 2 {
        let cell3 = collectionView.dequeueReusableCellWithReuseIdentifier("cell3", forIndexPath: indexPath) as! ThirdCollectionViewCell
        cell3.backgroundColor = UIColor.redColor()
        cell3.thirdAdd.addTarget(self, action: #selector(ViewController.importPicture(_:)), forControlEvents: .TouchUpInside)
        return cell3
    }
return returnCell

}

 func importPicture(sender: UIButton) {

    let point = myCollectionView.convertPoint(CGPoint.zero, fromView: sender)
    let indexPath = myCollectionView.indexPathForItemAtPoint(point)
    let cell1 = myCollectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath!) as! FirstCollectionViewCell

    currentPickerTarget = cell1.firstImages

    imagePicker.delegate = self
    imagePicker.sourceType = .PhotoLibrary
    imagePicker.allowsEditing = true

    presentViewController(imagePicker, animated: true, completion: nil)
}

func secondImport(sender: UIButton) {
    let point = myCollectionView.convertPoint(CGPoint.zero, fromView: sender)
    let indexPath = myCollectionView.indexPathForItemAtPoint(point)
    let cell2 = myCollectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath!) as! SecondCollectionViewCell

    print("\(indexPath!.row)")

    secondPickerTarget = cell2.secondImages
    secondPicker.delegate = self
    secondPicker.sourceType = .PhotoLibrary
    secondPicker.allowsEditing = true

    presentViewController(secondPicker, animated: true, completion: nil)

}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    imagePicker.dismissViewControllerAnimated(true, completion: nil)
    secondPicker.dismissViewControllerAnimated(true, completion: nil)

    let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
    currentPickerTarget.image = pickedImage
    secondPickerTarget.image = pickedImage
}

1 Answers1

0

You can make an array of default images or blank images and add a button on every cell above imageview. Assign tag to button according to cell. When user taps on any cell detect its count and then pick a picture from gallery or camera update object at index in array and reload collectionview or you can reload particular cell in collection view. Hope this helps :)

Deepak Chaudhary
  • 1,723
  • 1
  • 15
  • 29