我有这个代码试图在用户从他们的库中选择图像后更新自定义tableviewcell中的uiimageviews图像
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
let cell = tableView.dequeueReusableCellWithIdentifier("myJourneyPhotoCell") as! MyJourneyPhotoTableViewCell
cell.imgPhoto.image = image
cell.setCell(
image
)
tableView.reloadData()
CustomPhotoAlbum().saveImage(image)
self.dismissViewControllerAnimated(true, completion: nil)
}
我知道我不需要两者
cell.imgPhoto.image
和
cell.setCell
但我正在尝试这两种方法,但两种方法都没有奏效。我可以在tableviewcontroller和tableviewcell类中选择之后打印出图像,这样我就知道图像正在发送但它不会更新。
这是我的tableviewcell类
@IBOutlet weak var imgPhoto: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func setCell(
image: UIImage)
{
self.imgPhoto.image = image
// self.imgPhoto.image = UIImage(named: "Calendar Active") // Test code
print(image)
}
感谢任何帮助。
答案 0 :(得分:0)
这是因为您每次委托方法时都会将新单元格出列 调用didFinishPickingImage。您必须跟踪单击的单元格的索引路径,然后使用
let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyJourneyPhotoTableViewCell // Here the indexPth is the index path of your clicked cell
获取单元格的对象,然后设置其图像。
答案 1 :(得分:0)
您的图像未在tableViewCell中设置,因为根据您的代码,tableView会在设置图像之前重新加载。因此,在图像集之后添加重新加载tableview代码。
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
let cell = tableView.dequeueReusableCellWithIdentifier("myJourneyPhotoCell") as! MyJourneyPhotoTableViewCell
cell.imgPhoto.image = image
cell.setCell(
image
)
CustomPhotoAlbum().saveImage(image)
self.dismissViewControllerAnimated(true, completion: nil)
tableView.reloadData()
}
答案 2 :(得分:0)
在自定义单元格类中添加图像变量,如下所述:
var image : UIImage!
现在,在拾取器委托方法中将拾取的图像分配给此变量:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
let cell = tableView.dequeueReusableCellWithIdentifier("myJourneyPhotoCell") as! MyJourneyPhotoTableViewCell
cell.image = image
CustomPhotoAlbum().saveImage(image)
self.dismissViewControllerAnimated(true, completion: nil)
}
最后,您需要在自定义单元格类中添加以下方法
override func layoutSubviews() {
super.layoutSubviews();
if(image){
self.imgPhoto.image = image
}else{
self.imgPhoto.image = UIImage.init(named: "DefaultImage")
}
}
答案 3 :(得分:0)
尝试使用didFinishPickingMediaWithInfo
方法的UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
var image : UIImage!
image = info[UIImagePickerControllerOriginalImage] as? UIImage
let cell = tableView.dequeueReusableCellWithIdentifier("myJourneyPhotoCell") as! MyJourneyPhotoTableViewCell
cell.imgPhoto.image = image
dispatch_async(dispatch_get_main_queue()) {
tableView.reloadData()
}
CustomPhotoAlbum().saveImage(image)
self.dismissViewControllerAnimated(true, completion: nil)
}