先注冊
table?.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCellID")
//MARK:數(shù)據(jù)請求
func GETData() -> Void {
let url = "https://www.zhaoapi.cn/data/content.json"
Alamofire.request(url).responseJSON { (response) in
//轉化JSON
let json = JSON(response.value!)
//遍歷data值
for dict in json["data"].arrayValue{
let dic = dict.dictionary ?? [:]
let model:ViewModel = ViewModel()
model.title = dic["title"]?.stringValue ?? ""
model.desc = dic["desc"]?.stringValue ?? ""
model.id = dic["id"]?.stringValue ?? ""
for item in (dic["list"]?.arrayValue)!
{
let ite = item.dictionary ?? [:]
let listmodel:listModel = listModel()
listmodel.id = ite["id"]?.stringValue ?? ""
listmodel.extension0 = ite["extension"]?.stringValue ?? ""
listmodel.definition = ite["definition "]?.stringValue ?? ""
listmodel.title = ite["title "]?.stringValue ?? ""
listmodel.connotation = ite["connotation "]?.stringValue ?? ""
//添加到 model
model.list.append(listmodel)
}
print(model.title)
//將model添加到數(shù)據(jù)
self.dataArray.append(model)
}
//回到主線程刷新表格
OperationQueue.main.addOperation({
self.table?.reloadData()
})
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellID", for: indexPath) as! TableViewCell
let model = dataArray[indexPath.row]
cell.title.text = model.title
// let url = URL(string:"https://www.zhaoapi.cn/images/unit/1.png" )
//
let url = URL(string: "https://www.zhaoapi.cn/images/unit/(model.id).png")
// cell.icon.kf.setImage(with: url)
cell.icon.kf.setImage(with: url, placeholder: UIImage(named: "1"), options: nil, progressBlock: nil, completionHandler: nil)
return cell
}