【问题标题】:Selecting an image inside of TableViewCell选择 UITableViewCell 内的图像
【发布时间】:2016-03-02 11:23:09
【问题描述】:

我有一个 tableView,tableView 里面有 3 张图片。

我希望能够选择一个图像并被定向到另一个 VC,它显示已被点击的图像。

对于我的图像(从 Parse 下载),我有一个 uuid(唯一标识符),在我使用 collectionView 之前,我是这样设置的:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        postuuid.append(uuidArray[indexPath.row])

        let post = self.storyboard?.instantiateViewControllerWithIdentifier("collectionImage") as! CollectionImageViewController
        self.navigationController?.pushViewController(post, animated: true)
    }

这让我得到了我现在想要实现的目标......

我意识到我不能使用 TableView SelectedRow..... 因为这显然会选择整行,所以我设置了一个图像 UITapGestureRecognized 像这样:

 let imageTap = UITapGestureRecognizer(target: self, action: "imageTap")
            imageTap.numberOfTapsRequired = 1
            imageView.userInteractionEnabled = true
            imageView.addGestureRecognizer(imageTap)

然后使用 func() 将其重定向到新的 VC:

 func imageTap() {

        postuuid.append// My issues here, I'm not sure how to set it up or reference it to the cellForRow, like I did it in the collectionViewSelected item code above.

        let post = self.storyboard?.instantiateViewControllerWithIdentifier("collectionImage") as! CollectionImageViewController
        self.navigationController?.pushViewController(post, animated: true)
    }

正如您从我的代码中看到的那样,我不知道或目前似乎无法弄清楚如何将其连接到唯一 ID...

我已将 UUID 设置为 var = uuidArray = String,它正在正确下载它们...

CellForRow...:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
     {
        var counter = (indexPath.row * 3)

      let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ProfileTableViewCell




        for imageView in cell.threeImages {
            imageView.image = UIImage(named: "ImagePlaceHolder")

            if counter < imageArray.count {
            imageView.hidden = false
            let finalImage = imageArray[counter++]["image"]
            finalImage!.getDataInBackgroundWithBlock {
                (imageData: NSData?, error: NSError?) -> Void in
                if error == nil {
                    if let imageData = imageData {
                        imageView.image = UIImage(data:imageData)

                    }}}} else {
                imageView.hidden = true
            }



           let imageTap = UITapGestureRecognizer(target: self, action: "imageTap")
            imageTap.numberOfTapsRequired = 1
            imageView.userInteractionEnabled = true
            imageView.addGestureRecognizer(imageTap)


return cell
        }

如果有人能帮我解决这个问题,我提前非常感谢你!

最好的问候。

【问题讨论】:

  • 能不能显示你的cellfor rowatindexpath,很容易解决
  • 你也可以使用 UIButton 也很有用,你也可以得到它的事件。
  • @Anbu.Karthik 我已经更新了
  • @NitinGohel 我知道,但我更愿意先尝试这样做!
  • 为什么不能选择行?

标签: ios swift parse-platform tableview collectionview


【解决方案1】:

您可以添加 indexPath.row 作为 tapGesture 识别器的标签:

...
let imageTap = UITapGestureRecognizer(target: self, action: "imageTap:")
imageView.tag = indexPath.row
...

并将您的功能设置为

func imageTap(sender:AnyObject) {

    postuuid.append(uuidArray[sender.view.tag])

    let post = self.storyboard?.instantiateViewControllerWithIdentifier("collectionImage") as! CollectionImageViewController
    self.navigationController?.pushViewController(post, animated: true)
}

【讨论】:

  • 不让我添加标签..?
  • 对不起,您不能为手势添加标签,但您可以向 imageView 添加标签,以后可以作为 sender.view.tag 访问它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多