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
}