【问题标题】:Swift: set Image to Custom cell ImageViewSwift:将图像设置为自定义单元格 ImageView
【发布时间】:2017-01-24 13:53:45
【问题描述】:

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

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
}

我很困惑如何从这里调用单元格图像视图? 我该如何进一步处理?我只需要将获得的图像设置为单元格中的 imgeView 其他一切正常..

【问题讨论】:

  • 您添加的代码可能在 didFinishPickingMediaWithInfo 内,而不是在您的 Tapgesture 方法内。
  • 这里一切正常。我可以选择图像,但没有明白如何在单元格图像视图中设置
  • 这里一切正常。我可以选择图像,但没有明白如何在单元格图像视图中设置..是的,它在里面..

标签: ios swift uitableview uiimageview


【解决方案1】:

这是我刚刚测试过的一个例子

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

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")
}

这应该会立即更新图像,无需重新加载表格

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多