iOS-JSON数据的解析8.25

2018-08-25  本文已影响0人  长衣貌

数据请求部分

let urlString = "http://api.budejie.com/api/api_open.php"
        
        let parameters = [
            "a" : "list",
            "c" : "data",
            "type" : 41
            ] as [String : Any]
        
        PDNetworkTools.sharedInstance.request(.POST, urlString: urlString, parameters: parameters) { (data, error) in
            guard error == nil else {
                print(error as Any)
                return
            }
            print(data as Any)
            guard let dict = data else {
                return
            }
            let resultDict = dict as! NSDictionary
            let listArray = resultDict.value(forKey: "list") as! NSArray
            for item in listArray {
                let dict = item as! NSDictionary
                print(dict)
                let model = VideoModel(dict: dict as! [String : Any])
                self.dataSource.append(model)
            }
            self.myCollectionView.reloadData()
         }

网格的协议方法

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! MyCollectionViewCell
        cell.backgroundColor = UIColor.green
        cell.item = dataSource[indexPath.row]
        return cell
    }

自定义Cell(控件是用拖拽连线)

// MARK:- 控件的拖线属性
    @IBOutlet weak var BGView: UIView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var picView: UIImageView!
    @IBOutlet weak var playCountLabel: UILabel!
    
    // MARK:- 模型属性
//    var picURL : URL? {
//        didSet {
//            if let picURL = picURL {
////                iconView.setImageWith(picURL, placeholderImage: UIImage(named: "empty_picture"))
//            }
//        }
//    }
    
    var item : VideoModel? {
        didSet {
            let url = URL(string: (item?.bimageuri)!)
            picView.setImageWith(url!, placeholderImage: UIImage(named: "07"))
            titleLabel.text = item?.text
            playCountLabel.text = "播放:\(item?.playcount ?? "1")次"
        }
    }

数据模型

/** 视频的展示图片 */
    @objc var bimageuri : String?
    /** 视频标题 */
    @objc var text : String?
    @objc var playcount : String?
    
    init(dict : [String : Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }


上一篇下一篇

猜你喜欢

热点阅读