Don*_*one 4 uitableview ios swift2
我正在尝试使用 Alomofire 和 swiftyJSON 从 json 加载图像。Json 是字典:
{"name": {"image": "https://...",}}
Run Code Online (Sandbox Code Playgroud)
Alamorefire 和 SwiftyJSON
var imageLoad: String!
Alamofire.request(.GET, url).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)
if let jsonDict = json.dictionary {
let image = jsonDict["name"]!["image"].stringValue
print(image) // https://....
self.imageLoad = image
}
self.tableView.reloadData()
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TVCell
// Configure the cell...
cell.imageTableView.image = UIImage(named: imageLoad) // not working "fatal error: unexpectedly found nil while unwrapping an Optional value"
Run Code Online (Sandbox Code Playgroud)
如果有人可以帮忙吗?如果有其他方法可以随意写。
我分享了一个使用Swift 3和AlamofireImage实现的代码:
import UIKit
import AlamofireImage
class MovieViewCell: UICollectionViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var overviewLabel: UILabel!
@IBOutlet weak var posterView: UIImageView!
func configure(for movie: Movie) {
titleLabel.text = movie.title
overviewLabel.text = movie.overview
let url = URL(string: movie.poster_path!)!
posterView.af_setImage(withURL: url)
}
override func prepareForReuse() {
super.prepareForReuse()
posterView.af_cancelImageRequest()
posterView.image = nil
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您将需要Cocoapods:
platform :ios, '10.0'
inhibit_all_warnings!
use_frameworks!
target 'MovieApp' do
pod 'Alamofire', '~> 4.5'
pod 'AlamofireImage', '~> 3.3'
end
Run Code Online (Sandbox Code Playgroud)
官方文档AlamofireImage和ImageCell.swift 示例
| 归档时间: |
|
| 查看次数: |
10883 次 |
| 最近记录: |