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
}