从SDWebimage加载/下载图像并在Custom tableView cell imageView中显示

时间:2018-06-01 06:28:05

标签: uitableview swift3 sdwebimage

我已经解析了其他值,但我将如何向imageView显示图像我已经在Swift中使用SdWebImage。我想在底部单元格中显示“详细信息”下面是我的代码

import UIKit
import SystemConfiguration
import MBProgressHUD


public struct Section {
    var arrayDataTop: String
    var arrayTerms: String
    var qrImage:String
    var collapsed: Bool

    public init( arrayDataTop: String,qrImage: String ,arrayTerms: String, collapsed: Bool = false) {
        self.arrayDataTop = arrayDataTop
        self.qrImage = qrImage
        self.arrayTerms = arrayTerms
        self.collapsed = collapsed
    }
}

class CollapsibleViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableViewCollapsible:UITableView!
@IBOutlet weak var listImage:UIImageView!

var nodatastr:String = "No Deal Found."
var dealIDCollapsible : String?
var dealDictCollapsible = [String: AnyObject]()
var parentNavigationController: UINavigationController?
private var loadingView:MBProgressHUD?

var sectionDataObj = [Section]()


override func viewDidLoad() {
    super.viewDidLoad()

    if (!self.isInternetAvailable()){

        self.alertMessageShow(title: "No Internet Connection", message: "Make sure your device is connected to the internet.")
    }
    else{
        if self.loadingView == nil {
            self.loadingView =  MBProgressHUD.showAdded(to: self.view, animated: true)
        }


        tableViewCollapsible.estimatedRowHeight = 100.0
        tableViewCollapsible.layoutIfNeeded()
        tableViewCollapsible.updateConstraintsIfNeeded()
        tableViewCollapsible.tableFooterView = UIView()


        self.listImageFetch()
        dealFetchParticularListing()
    }
}








func dealFetchParticularListing(){

    let prs = [
        "listing_id":dealIDCollapsible,//dealIDCollapsible,
        "Deal_fetch_listing": "1" as String
    ]
    Service.CreateDeal(prs as [String : AnyObject]?, onCompletion: { result in
        let json = result as? NSDictionary
        if let data = json as? [String:Any]{
            if let err = data["status"] as? String, err == "success"{
                if let data = data["result"] as? [Any]{
                    //
                    //fill your data in that local Section obj
                    //
                    var sectionDataObj = [Section]()
                    for sectionObj in data{
                        if let sectionObjVal = sectionObj as? [String:Any]{
                            if let qrcode = sectionObjVal["qrcode"] as? String{
                                if let tnc = sectionObjVal["tnc"] as? String{
                                    if let deal_title = sectionObjVal["deal_title"] as? String{
                                        let sectionValue = Section(arrayDataTop: deal_title, qrImage: qrcode, arrayTerms: tnc)
                                        // access main objects/UIelement on main thread ONLY
                                        sectionDataObj.append(sectionValue)

                                    }
                                }
                            }
                        }
                    }
                    DispatchQueue.main.async { () -> Void in
                        self.sectionDataObj.removeAll()
                        //
                        //assign ur data in main sampleData(Section obj) then reload tableView with that data.
                        //
                        self.sectionDataObj = sectionDataObj
                        self.tableViewCollapsible.reloadData()
                        self.loadingView?.hide(true)
                    }
                }
            }
        }
    })
}

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


    // Header
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! MyCellData
        cell.lblDealTitle.text = sectionDataObj[indexPath.section].arrayDataTop
        return cell
    }
    // Cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "detail") as! MyCellData
    cell.lblTerm.text = sectionDataObj[indexPath.section].arrayTerms
    // here i want to show image 
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension//320.0
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        let collapsed = !sectionDataObj[indexPath.section].collapsed
        // Toggle collapse
        sectionDataObj[indexPath.section].collapsed = collapsed
        self.tableViewCollapsible.reloadSections([indexPath.section], with: .automatic)
    }
}
 }



class MyCellData:UITableViewCell{

    @IBOutlet weak var lblDealTitle: UILabel!
    @IBOutlet weak var dealimage: UIImageView!
    @IBOutlet weak var lblTerm: UILabel!
    @IBOutlet weak var qrCodeImage: UIImageView!

}

Plz帮我解决这个问题。提前做好准备。所有事情都正常工作我能看到其他细节。帮助将会受到赞赏.Plz帮助我解决这个问题。

1 个答案:

答案 0 :(得分:1)

更新你的tableView cellForRowAt,以便从你的 sectionDataObj

中显示图片或你的手机
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    // Header
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! MyCellData
        cell.lblDealTitle.text = sectionDataObj[indexPath.section].arrayDataTop
        return cell
    }
    // Cell
    let cell = tableView.dequeueReusableCell(withIdentifier: "detail") as! MyCellData
    cell.lblTerm.text = sectionDataObj[indexPath.section].arrayTerms
    let imgUrl = sectionDataObj[indexPath.section]. qrImage
    cell.qrCodeImage.sd_setImage(with: URL(string:imgUrl), completed: nil)
    return cell
}