在这篇博文中你可以看到那些内容呢?首先这是一个用tableView纯代码Swift写的简单界面,你可以看到下面这些:

  • 使用Alamofire 进行网络请求
  • 使用MJExtension 进行字典转模型
  • 使用HanekeSwift 进行图片的赋值
  • 如何写一个模型(M)
  • 如何自定义一个UITableViewCell


iOS实战:用纯Swift写一个完整的界面,Alamofire MJExtension HanekeSwift Model

iOS实战:用纯Swift写一个完整的界面,Alamofire MJExtension Haneke - 敏捷大拇指 - iOS实战:用纯Swift写一个完整的界面,Alamofire MJExtension HanekeSwift Model

演示




1、Alamofire 简单网络请求

[Swift] 纯文本查看 复制代码
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_ 链接




2、如何写一个模型

[Swift] 纯文本查看 复制代码
class AlamofireVCModel: NSObject {
    var id:       NSInteger = 0
    var title:    String?
    var pic:      String?
    var readnum:  String?
    var replynum: String?
}





3、如何自定义一个Cell

[Swift] 纯文本查看 复制代码
    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
    }






作者:夏天然后