2016-09-10 6 views
0

현재 사용자가 셀 내의 버튼을 탭하여 원하는 콜렉션 뷰 셀에 이미지를 업로드 할 수 있다는 목표로 애플리케이션을 작성 중입니다. 지금까지 나는 이미지를 선택할 수 있었지만 지정된 셀에는 절대 가지 않을 것입니다. 질문의 편의를 위해 처음 세 개의 셀에 사용한 코드를 보여줍니다.이미지 선택기를 사용하여 두 개의 콜렉션 뷰 셀에 이미지로드하기

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

기본 이미지 또는 빈 이미지의 배열을 만들고 이미지 뷰 위에있는 모든 셀에 버튼을 추가 할 수 있습니다. 셀에 따라 버튼에 태그를 지정합니다. 셀에서 사용자 탭을 클릭하면 셀 수를 감지하고 갤러리 또는 카메라 업데이트 개체의 그림을 배열의 색인에서 선택하고 collectionview를 다시로드하거나 특정 셀을 컬렉션보기에서 다시로드 할 수 있습니다. 희망이 있습니다.

관련 문제