我希望能够允许用户在选中图片库后将其从图片库上传到CollectionView中的特定单元格。我这里有两个问题。
(1)我不确定我是否正确调用了cellTapped()
函数。
(2)我希望能够使用标签来存储indexpath.row,但不确定如何在imagepickercontroller
函数中调用标签。
感谢帮助。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell: UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
cell.layer.borderWidth = 4.0
cell.layer.borderColor = UIColor.blue.cgColor
cellTapped()
}
func cellTapped(){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
{
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
dismiss(animated: true, completion: nil)
collectionView?.reloadData()
}
答案 0 :(得分:0)
在拾取图像之前,您需要记住轻触了哪个单元格。您可以将索引保存在collectionView(_ :, didSelectItemAt:)
Map
然后,在拾取图像并重新加载集合时,您需要更新数据源。
// instance property
private var selectedCellIndexPath: IndexPath?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedCellIndexPath = indexPath
let cell: UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
cell.layer.borderWidth = 4.0
cell.layer.borderColor = UIColor.blue.cgColor
cellTapped()
}