我目前正在构建一个应用程序,目标是用户可以通过点击单元格内的按钮将图像上传到所需的Collection View Cell。到目前为止,我已经能够选择一个图像,但它永远不会去指定的单元格。为了便于解决,我将展示我用于前三个单元格的代码。
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 :(得分:0)
您可以制作一组默认图像或空白图像,并在imageview上方的每个单元格上添加一个按钮。根据单元格将标签分配给按钮。 当用户点击任何单元格检测其计数,然后从数组索引中的图库或相机更新对象中选择图片并重新加载集合视图,或者您可以在集合视图中重新加载特定单元格。 希望这会有所帮助:)