簡體   English   中英

Swift:將Image設置為Custom cell ImageView

[英]Swift: set Image to Custom cell ImageView

我想將“圖像”設置為“自定義”單元格。 我對如何做到這一點感到困惑。 可以說我的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
}

我很困惑如何從這里調用單元格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")
}

這應該立即更新映像,而無需重新加載表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM