使用Swift 3和Alamofire,JSON url中的应用程序中没有显示任何内容
我有一个使用Swift 3和Alamofire的应用程序。 数据连接到两个cell?.viewWithTag(2)
和cell?.viewWithTag(1)
,它是一个图像(来自url)和文本。 所以当我运行项目时,我的应用程序中没有显示任何内容。 我已经使用print(readableJSON)
测试了JSON,并且JSON正在打印到控制台中。 所以我真的有点困惑。 我的swift看起来像这样:
迅速
import UIKit import Alamofire struct postinput { let mainImage : UIImage! let name : String! } class TableViewController: UITableViewController { var postsinput = [postinput]() var mainURL = "https://www.example.api.com" typealias JSONstandard = [String : AnyObject] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. callAlamo(url: mainURL) } func callAlamo(url : String){ Alamofire.request(url).responseJSON(completionHandler: { response in self.parseData(JSONData: response.data!) }) } func parseData(JSONData : Data) { do { var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard print(readableJSON) if let posts = readableJSON["posts"] as? [JSONstandard] { for post in posts { let title = post["title"] as! String print(title) if let imageUrl = post["image"] as? JSONstandard { let mainImageURL = URL(string: imageUrl["url"] as! String) let mainImageData = NSData(contentsOf: mainImageURL!) let mainImage = UIImage(data: mainImageData as! Data) postsinput.append(postinput.init(mainImage: mainImage, name: title)) self.tableView.reloadData() } } } } catch { print(error) } } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return postsinput.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") // cell?.textLabel?.text = titles[indexPath.row] let mainImageView = cell?.viewWithTag(2) as! UIImageView mainImageView.image = postsinput[indexPath.row].mainImage let mainLabel = cell?.viewWithTag(1) as! UILabel mainLabel.text = postsinput[indexPath.row].name return cell! } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
JSON
{ "posts" : [{ "id": "000000", "url": "/content/interview2", "date": "2016-11-03 09:01:41", "modified": "2016-11-03 09:03:47", "title": "An interview", "image": "https://www.example.com/sites/default/files/oregood.jpeg", "summary": { "value": "Latin text here
", "format": "filtered_html" } }] }
从您的JSON响应image
键包含String
而不是Dictionary
也需要重新加载您的tableView
外部循环不是每次在循环内,所以尝试这样。
if let posts = readableJSON["posts"] as? [JSONstandard] { for post in posts { let title = post["title"] as! String if let imageUrl = post["image"] as? String { let mainImageURL = URL(string: imageUrl as! String) let mainImageData = NSData(contentsOf: mainImageURL!) let mainImage = UIImage(data: mainImageData as! Data) postsinput.append(postinput.init(mainImage: mainImage, name: title)) } } DispatchQueue.main.async { self.tableView.reloadData() } }
建议:不要在主线程击球SDWebImages
使用NSData(contentsOf:)
下载图像来使用像SDWebImages
这样的SDWebImages
,也可以创建自己的异步图像下载器。
因为您没有在表格单元格中显示JSON。 您创建一个名为“mainImageView”的复制对象,并且永远不会将其指定为表格单元格的实际imageView,而是尝试:
(cell?.viewWithTag(2) as? UIImageView).image = postsinput[indexPath.row].mainImage
postsinput包含图像字符串或图像?
编辑:所以它会是:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") // cell?.textLabel?.text = titles[indexPath.row] //let mainImageView = cell?.viewWithTag(2) as! UIImageView //mainImageView.image = postsinput[indexPath.row].mainImage (cell?.viewWithTag(2) as? UIImageView).image = postsinput[indexPath.row].mainImage //let mainLabel = cell?.viewWithTag(1) as! UILabel //mainLabel.text = postsinput[indexPath.row].name (cell?.viewWithTag(1) as? UILabel).text = postsinput[indexPath.row].name return cell! }
- 在iOS Swift 2中实现“Read More”按钮扩展UITextView的最简单方法是什么?
- RestKit和AFNetworking生成错误
- 如何使用Swift 4(滑动选项卡/段)在iOS中实现类似于Android的选项卡布局
- 确定二进制文件是否包含Bitcode的方法似乎不再适用
- 确定UIImage的主要和次要颜色
- 为什么设置整数时会发生EXC_BAD_ACCESS和SIGABRT?
- embedded到容器视图中的tableView顶部的空白区域
- 强制整个屏幕上的横向方向MPMoviePlayerController在退出全屏时阻止正确的旋转
- 如何将NSTextView的格式化内容转换为string