gpt4 book ai didi

ios - 想要从仅包含图像名称的 api 将图像显示到 tableview 单元格中

转载 作者:行者123 更新时间:2023-11-29 05:46:03 25 4
gpt4 key购买 nike

我有一个 API,其中仅给出了 image_name。如何将图像加载到 UITableViewCell 中。我正在使用 Alamofire 和 SwiftyJSON。我收到 hotal_originalnamehotal_address 但没有图像。我已经尝试过了,但没有成功。

到目前为止我尝试过的代码:

hotelModel 结构:

struct hotelModel {
var hotal_originalname:String = ""
var hotal_image:String = ""
var hotal_address:String = ""

init(json:JSON) {
hotal_originalname = json["hotal_originalname"].stringValue
hotal_image = json["hotal_image"].stringValue
hotal_address = json["hotal_address"].stringValue
}
}

使用 Alamofire 代码进行编码:

class ListViewController: UIViewController{

let BASE_URL = "https://socialinfotech.in/development/ExcelReport/api/v1/hotel"
let headers: HTTPHeaders = [
"X-Authorization": "2933c869ebe0a3e42a068ec50e305db5",
"Content-Type": "application/x-www-form-urlencoded"
]

var arrData = [hotelModel]()

// MARK: IBOutlets
@IBOutlet weak var listViewTable: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

//set gradient to navigation bar
SHNDNavigationBarGradient(firstColor: #colorLiteral(red: 0.3450980392, green: 0.737254902, blue: 0.9568627451, alpha: 1), secondColor: #colorLiteral(red: 0.262745098, green: 0.6078431373, blue: 0.7921568627, alpha: 1))
jsonParsing()

}

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

func jsonParsing() {

Alamofire.request(BASE_URL, method: .get, headers: headers).responseJSON { (response)-> Void in
debugPrint(response)

if(response.result.value) != nil {
let json = JSON(response.result.value!)
let results = json["Result"]
for arr in results.arrayValue {
self.arrData.append(hotelModel(json: arr))
print(self.arrData)
}
DispatchQueue.main.async {
self.listViewTable.reloadData()
}
}


}

}

//MARK: IBActions
@IBAction func logoutBtnTapped(_ sender: UIBarButtonItem) {

UserDefaults.standard.set(false, forKey: "ISUSERLOGGEDIN")
self.navigationController?.popToRootViewController(animated: true)

}
}

extension ListViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell", for: indexPath) as! ListCell

let imgUrl = "http://socialinfotech.in/development/ExcelReport/uploads/original/"
let url = imgUrl + "uploads-35766-1557402138-20190509-114218am.jpg"


cell.hotelImage.kf.setImage(with: URL(string: url))
cell.hotelName.text = arrData[indexPath.row].hotal_originalname
cell.hotelAddress.text = arrData[indexPath.row].hotal_address
//
// Alamofire.request(url).responseImage { (response) in
// if let image = response.result.value {
// cell.hotelImage.image = image
// }
// }
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let chartVC = self.storyboard?.instantiateViewController(withIdentifier: "ChartViewController") as! ChartViewController
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.pushViewController(chartVC, animated:true)
}

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
}

最佳答案

您正在使用静态图像网址。

替换这一行

    let imgUrl = "http://socialinfotech.in/development/ExcelReport/uploads/original/"
let url = imgUrl + "uploads-35766-1557402138-20190509-114218am.jpg"

    let imgUrl = "http://socialinfotech.in/development/ExcelReport/uploads/original/"
let url = imgUrl + arrData[indexPath.row].hotal_image

希望对你有帮助

关于ios - 想要从仅包含图像名称的 api 将图像显示到 tableview 单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56141713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com