【发布时间】:2018-05-17 16:21:02
【问题描述】:
大家好,因为我是新生,所以不知道如何从 API 获取图像和 pdf 文件以及如何在 tableviewcell 中显示,以便用户可以看到图像和 pdf 文件以及从那里下载它。我尝试了一些代码图像,但它不工作,对 pdf 没有一个想法。请帮助。
图像代码: 我已经声明了一组图像,如下所示:
var announimage : Array = [UIImage]()
以下是我的 JSON 数据格式。
{
"Download_Details": [
{
"school_id": 1,
"class_id": "Pre - KG ",
"section": "A ",
"file_name": "427a3be0155b49db999cffb51f078f9c_431875732.pdf",
"title": "TeQ",
"message": "Hello Users",
"date": null
},
{
"school_id": 1,
"class_id": "Pre - KG ",
"section": "A ",
"file_name": "dce5ab53f45f4ec5b52894bc6286ed35_1376021.jpg",
"title": "Welcome",
"message": "Welcomes you.",
"date": null
},
{
"school_id": 1,
"class_id": "Pre - KG ",
"section": "A ",
"file_name": "9d3ff19bd54e48a087947227ee36c68c_13417773.png",
"title": "TEST",
"message": "TEST",
"date": null
}
],
}
以下是我尝试过的代码:
let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
let downarray = responseString?.value(forKey: "Download_Details") as? NSArray
for down in downarray!
{
let dowdict = down as? NSDictionary
let dmsgname = dowdict?.value(forKey: "message")
self.announmessage.append(dmsgname as! String)
let downimage = dowdict?.value(forKey: "file_name")
self.announimage.append(downimage as! UIImage)
}
DispatchQueue.main.async
{
self.annountable.reloadData()
}
UITableView 数据源:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! AnnounceCellTableViewCell
cell2.announlabel.text = announmessage[indexPath.row]
cell2.announdate.text = announcedates[indexPath.row]
let image :UIImage = UIImage(data:announimage[indexPath.row] as! Data)!
cell2.dataimage.image = image
return cell2
}
【问题讨论】:
-
一个 UIWebView 可以显示 pdf。并且文件名需要一个完整的 url,例如“http://www.../file.png”
-
@deltami:在这里给我看看你的 tableview dataSource 方法
-
@Bucket Question 用数据源方法更新。
-
let downimage = dowdict?.value(forKey: "file_name") 将只获得 downimage 变量中的字符串,即; “9d3ff19bd54e48a087947227ee36c68c_13417773.png”不是图片。
-
@deltami 图片的完整 URL 字符串是什么?您从哪个 url 获得这些 json 响应。
标签: json uitableview swift3 uiimageview