效果:
ViewController.swift
import UIKit
import SwiftyJSON
import Alamofire
import Kingfisher
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableview: UITableView!
var books = [Book]()
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://api.douban.com/v2/book/search"
Alamofire.request(.GET, urlString, parameters: ["tag":"Swift","count":10]).validate().responseJSON { (resp) in
if let error = resp.result.error{
print(error)
}else if let value = resp.result.value,jsonArray = JSON(value)["books"].array{
for json in jsonArray{
let book = Book()
book.title = json["title"].string ?? ""
book.subtitle = json["subtitle"].string ?? ""
book.image = json["image"].string ?? ""
self.books.append(book)
}
self.tableview.reloadData()
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCellWithIdentifier("Cell")!
let book = books[indexPath.row]
cell.textLabel?.text = book.title
cell.detailTextLabel?.text = book.subtitle
cell.imageView?.kf_setImageWithURL(NSURL(string: book.image) ?? NSURL(),placeholderImage:UIImage(named: "cake") )
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return books.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableview.deselectRowAtIndexPath(indexPath, animated: false)
let url = books[indexPath.row].image
self.performSegueWithIdentifier("DetailViewController", sender: url)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "DetailViewController"{
let controller = segue.destinationViewController as! DetailViewController
controller.url = sender as? String
}
}
}
class Book:NSObject{
var title=""
var subtitle=""
var image = ""
}
DetailViewController
import UIKit
import Alamofire
class DetailViewController: UIViewController {
@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var label: UILabel!
var imagePath = ""
var url:String!{
didSet{
Alamofire.download(.GET, url){temporaryURL, response in
let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
self.imagePath=directoryURL.URLByAppendingPathComponent(pathComponent!).path!
if fileManager.fileExistsAtPath(self.imagePath){
try!fileManager.removeItemAtPath(self.imagePath)
}
return directoryURL.URLByAppendingPathComponent(pathComponent!)
}.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead ) in
let progess = Int(Double(totalBytesRead)/Double(totalBytesExpectedToRead) * 100)
dispatch_async(dispatch_get_main_queue()) {
self.label.text = "\(progess)%"
}
}.response(queue: dispatch_get_main_queue()) { (_, _, _, error)->Void in
if let error = error{
print(error)
}else{
self.label.hidden = true
self.imageview.image = UIImage(contentsOfFile: self.imagePath)
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func tapview(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}