У меня есть API, в котором указывается только image_name
.Как мне загрузить изображение в UITableViewCell
.Я использую Alamofire и SwiftyJSON для этого.Я получаю hotal_originalname
и hotal_address
, но нет изображения.Я пробовал, но не получил его.
Код, который я пробовал до сих пор:
hotelModel struct:
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
}
}