ios - Swift:将图像设置为自定义单元格ImageView

标签 ios swift uitableview uiimageview

我想将图像设置为自定义单元格。我很困惑如何做到这一点。 假设我的 UITable View 中有一个自定义单元格。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell
    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(imageTapped(img:)))
    cell.imageView.isUserInteractionEnabled = true
    cell.imageView.addGestureRecognizer(tapGestureRecognizer)

    return cell
}

现在,只要我点击图像,就会调用以下函数:

func imageTapped(img: AnyObject){
    print("Image clicked.")

    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let imageData: NSData = UIImagePNGRepresentation(myImage)
        let image: UIImage = UIImage(data:imageData,scale:1.0)
        print(image)
        //i am uploading the photo from here. and i want to set this  picture to my cell imageView.
    }

    dismissViewControllerAnimated(true, completion: nil
}

我很困惑如何从这里调用单元 ImageView ? 我怎样才能进一步进行?我只需要将获得的图像设置为单元格中的 imgeView,其他一切正常..

最佳答案

这是我刚刚测试过的示例

我有一个名为ImageTableViewCell的自定义TableViewCell,其中包含一个名为customImageView的UIImageView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellImage", for: indexPath) as! ImageTableViewCell

    // initialising with some dummy data of mine
    cell.customImageView.image = UIImage(named: "Image\(indexPath.row).png")

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(_:)))
    cell.addGestureRecognizer(tapGesture)

    return cell
}

然后在 ViewController 中

func imageTapped(_ sender: UITapGestureRecognizer)
{
    let myCell = sender.view as! ImageTableViewCell
    myCell.customImageView.image = UIImage(named: "imageTapped.jpg")
}

这应该立即更新图像,而不需要重新加载表格

关于ios - Swift:将图像设置为自定义单元格ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41830009/

相关文章:

ios - webRTC 中对等连接状态始终保持在 "RTCICEConnectionChecking"状态

swift - 在 XCode、iOS 开发中编译时 Keychain.swift 中生成错误

ios - 将字符串转换为 Int 数组

ios - searchResultsUpdater 的其他 ViewController

ios - 在 UITableView 中将行高设置为 0,但单元格文本仍然显示重叠

iphone - 自定义 UITableViewCell 在被拖动重新排序时损坏

ios - "fatal error"调用tableView.reloadData()时,tableView已正确连接

ios - 如何在这些 UIImageView 之间保持图像的位置和大小不变?

ios - 你能从 iOS 应用程序向 Roku 显示数字内容吗(类似于 airplay)

ios - 使用 MPMediaQuery 加载播放列表