首页 文章

Swift根据检索到的firebase数据在TableView上设置图像

提问于
浏览
0

所以我构建检索数据的方式是:

struct framePic {let imagedB:Data!让priceB:String! }

我设法检索数据,但我不知道如何在tableView上设置图像?我已经设法用标签弄清楚了......但不是用图像?

这是表格中单元格的代码;

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell: RowCellTableView = myTableView.dequeueReusableCell(withIdentifier: "cell") as! RowCellTableView

     // sets label
    cell.priceView.text = framePics[indexPath.row].pricedB

    // set image on UIimageView???
    --- I dont know how to add this? I need somehow to get the list of only the images???  I believe if I had the array variable like; imageFiles[] I could have done;
imageFiles[indexPath.row].getDataInBackground... But in my case I use struct so how am I supposed to do this in this case?

RowCellTableView只包含;

@IBOutlet弱var imageView:UIImageView! @IBOutlet弱变价查看:UILabel!

2 回答

  • 0

    在您的结构中,您有:

    struct framePic { 
       let imagedB : Data! 
       let pricedB : String! 
    }
    

    尝试将imagedB转换为UIImage,如下所示:

    var imageUIImage: UIImage = UIImage(data: imageData)
    

    所以你的实现将是这样的:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
        let cell: RowCellTableView = myTableView.dequeueReusableCell(withIdentifier: "cell") as! RowCellTableView
    
         // sets label
        cell.priceView.text = framePics[indexPath.row].pricedB
    
        // convert imageData to UIImage
        var imageUIImage: UIImage = UIImage(data: framePics[indexPath.row].imagedB)
    
        // apply image to UIImageView
        cell.imageView.image = imageUIImage
    
        return cell
    }
    
  • -1
    cell.imageView.image = someImage
    

相关问题