swift Demo

import UIKit
import SnapKit
import Alamofire
import SwiftyJSON
import KakaJSON
class xhwVC: UIViewController {
    var dataList : [XHWModel] = []
    var url:String = "http://iappfree.candou.com:8080/free/applications/limited"
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupUI()
        self.loadData()
    }
    func setupUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
   
    func loadData()  {
        AF.request(url) .responseJSON { [self] (response) in
            debugPrint(response)
            let aaa:[String :Any] = response.value as! [String : Any]
            let bbb:[Any] = aaa["applications"] as! [Any]
            // 第一种
            let arr = bbb.kj.modelArray(XHWModel.self)
            dataList = arr
            //第二种
//            for ccc:[String :Any] in bbb as! [[String :Any]]{
//                let model4:XHWModel = ccc.kj.model(type: XHWModel.self) as! XHWModel
//                dataList.append(model4)
//            }
            tableView.reloadData()
        }
    }

    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(XHWCell.self, forCellReuseIdentifier: "xhwcell")
        return tableView
    }()

    
   

}
extension xhwVC: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:XHWCell = tableView.dequeueReusableCell(withIdentifier: "xhwcell", for: indexPath) as! XHWCell
        //第一种
//        cell.loadCellData(item: dataList[indexPath.row])
        //第二种
        cell.model = dataList[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
}
import UIKit
import SnapKit
import Kingfisher
//pod 'Kingfisher'
class XHWCell: UITableViewCell {
//    var model:XHWModel
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func loadCellData(item:XHWModel)  {
        self.nameLabel.text = item.name
        self.imageV.kf.setImage(with: URL(string: item.iconUrl))
    }
    
    var model:XHWModel = XHWModel() {
        didSet {
            self.nameLabel.text = model.name
            self.imageV.kf.setImage(with: URL(string: model.iconUrl))
        }
    }
    
    
    lazy var imageV: UIImageView = {
        let imageV = UIImageView()
        self.contentView.addSubview(imageV)
        imageV.backgroundColor = UIColor.brown
        return imageV
    }()
    
    lazy var nameLabel: UILabel = {
        let nameLabel = UILabel()
        self.contentView.addSubview(nameLabel)
        return nameLabel
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.imageV.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
            make.height.equalTo(40)
            make.width.equalTo(40)
        }
        
        self.nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.imageV.snp_rightMargin).offset(20)
            make.centerY.equalTo(self.imageV)
        }
    }
    
 
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

import UIKit
import SwiftyJSON
import KakaJSON
class XHWModel: Convertible {

    var applicationId: String!
    var slug: String!
    var name: String!
    var releaseDate: String!
    var version: String!
    var description: String!
    var categoryId: Int = 0
    var categoryName: String!
    var iconUrl: String!
    var itunesUrl: String!
    var starCurrent: String!
    var starOverall: String!
    var ratingOverall: String!
    var downloads: String!
    var currentPrice: String!
    var lastPrice: String!
    var priceTrend: String!
    var expireDatetime: String!
    var releaseNotes: String!
    var updateDate: String!
    var fileSize: String!
    var ipa: String!
    var shares: String!
    var favorites: String!
    required init() {
        
    }
    
}

你可能感兴趣的:(swift Demo)