2016-09-10 4 views
0

現在、ユーザーは、セル内のボタンをタップして、画像を目的のコレクションビューセルにアップロードできるという目標を持つアプリケーションを構築しています。これまで私は画像を選択することができましたが、指定されたセルには決して行きません。質問の容易さのために、最初の3つのセルに使用したコードを示します。イメージピッカーを使用して2つのコレクションビューセルに画像をロードする

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 
} 

答えて

0

デフォルト画像または空白画像の配列を作成し、画像ビューの上にあるすべてのセルにボタンを追加できます。セルに応じてボタンにタグを割り当てます。 任意のセルのユーザがタップするとカウントが検出され、ギャラリーまたはカメラの更新オブジェクトからピクチャを選択し、配列のインデックスおよびコレクションビューを再ロードするか、コレクションビューで特定のセルをリロードできます。 これが役に立ちますようお願いいたします:)

関連する問題