我在tableViewcell中有一个包含图像的按钮。我想获得在点击上一张图像(即一个按钮)时更改图像的选项,例如打开画廊或照相机。我正在使用以下代码来实现此目的: 在tableView单元格中:
protocol Profile2 : class{
func OnClick_2(index : Int)
}
class TableViewCell: UITableViewCell {
weak var celldelegate : Profile2?
var index : IndexPath?
}
在View Controller中
@IBAction func changeProfileImage(_ sender: Any) {
let action_sheet = UIAlertController(title: "Click to choose photo", message: nil, preferredStyle: .actionSheet)
action_sheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (action) in
self.ShowAlbum()
}))
action_sheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
}
//Show album method
func ShowAlbum()
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
imagePicker.allowsEditing = true
}
//In cell for row method:
cell.celldelegate = self
cell.index = indexPath
cell.profilePicture.isUserInteractionEnabled = true
//imagepicker method
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
})
let cell = tableView.dequeueReusableCell(withIdentifier: "profilePictureCell") as! TableViewCell
cell.profilePicture.imageView?.image = image
}
在点击按钮时会打开操作表,以便从图库中上传图像,但是在选择图像并进行上传时,按钮并没有设置。 请建议我如何解决或以其他方式解决。