在這篇博文中你可以看到那些內容呢, 首先這是一個用tableView純程式碼Swift寫的簡單介面, 你可以看到下面這些
- 使用Alamofire 進行網路請求
- 使用MJExtension 進行字典轉模型
- 使用HanekeSwift 進行圖片的賦值
- 如何寫一個模型(M)
- 如何自定義一個UITableViewCell
Alamofire 簡單網路請求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
func XTNetworkReq(url: String){ print("SUMMER_TEST_1") Alamofire.request(.GET, url, parameters: nil) .validate(contentType: ["application/json", "text/json", "text/javascript", "text/html","text/css", "text/plain","application/x-javascript"]) .responseJSON { response in switch response.result { case .Success: // print("Validation Successful: \(response.result.value)") // json 轉化成字典 // 並進行資料解析 let jsonDataDic = response.result.value as? NSDictionary let dataDic = jsonDataDic?.objectForKey("data") let listArray = dataDic?.objectForKey("list") print(listArray) // 字典轉模型(MJExtension) self.dataArray = AlamofireVCModel.mj_objectArrayWithKeyValuesArray(listArray) if self.dataArray.count > 0 { // 進行UI重新整理操作 self.tableView.reloadData() } case .Failure(let error): print(error) } } } |
參照 Alamofire API_ 連結
參照 MJExtension API_ 連結
如何寫一個模型
1 2 3 4 5 6 7 |
class AlamofireVCModel: NSObject { var id: Int = 0 var title: String? var pic: String? var readnum: String? var replynum: String? } |
如何自定義一個Cell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
var alaModel = AlamofireVCModel() var imagePic = UIImageView() var labelTitle = UILabel() override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier) self.createCellSubviews() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } // MARK: - 佈局UI func createCellSubviews(){ imagePic = UIImageView.init(frame: CGRectMake(5, 5, self.contentView.frame.size.width - 10, 188 - 10)) self.contentView.addSubview(imagePic) labelTitle = UILabel.init(frame: CGRectMake(10, 10, s_w, 60)) labelTitle.numberOfLines = 0 labelTitle.textColor = UIColor ( red: 1.0, green: 0.4666, blue: 0.3002, alpha: 1.0 ) imagePic.addSubview(labelTitle) } // MARK: - layoutSubviews override func layoutSubviews() { if (self.alaModel.pic != nil && self.alaModel.pic?.hasSuffix("gif") != nil){ // 進行圖片的賦值, 這裡給出的擷取字串的方法 self.imagePic.hnk_setImageFromURL(NSURL.init(string: (self.alaModel.pic?.substringToIndex((self.alaModel.pic?.startIndex.advancedBy(66))!))!)!) } self.labelTitle.text = self.alaModel.title } |