今天讲解在子控制器中获取数据,OC中我们用AFN框架请求数据,在Swift中我用的是Alamofire.当然这两个框架的功能差异性不大,并且都是一个作者写的,今天我就讲解如何使用该框架:
// 请求参数
let params = NSMutableDictionary()
params["a"] = "list"
params["c"] = "data"
params["type"] = self.Mycard()
weak var weakSelf = self
Alamofire.request(.GET, XYrequestUrl, parameters:NSDictionary(dictionary: params) as? [String: AnyObject]).responseJSON() { response in
switch response.result{
case .Success:
let JSON = response.result.value
//得到字典数组数据
let TextModel = JSON!.valueForKey("list") as? NSArray
// 字典数组 -> 模型数组
weakSelf?.dataArr = XYTextModel.mj_objectArrayWithKeyValuesArray(TextModel)
// 存储maxtime
let infoJson = JSON!["info"]! as? NSDictionary
weakSelf?.nextData = infoJson!["maxtime"] as? String
// 刷新表格
weakSelf?.tableView.reloadData()
// 结束刷新
weakSelf?.tableView.mj_header.endRefreshing()
case .Failure(let error):
print(error)
weakSelf?.tableView.mj_header.endRefreshing()
}
}
大致请求方式相同,如果是Post就直接将.get改为.Post即可,之后将获取数据在cell中转为模型数据
2:下载图片我用的是Kingfisher
用法1:
imageUser.kf_setImageWithURL(NSURL(string: selectModel!.profile_image!)!, placeholderImage: UIImage(named: "defaultUserIcon"))
用法2:
<pre name="code" class="objc"> self.progrssImage.setProgress(0.0, animated: false)
//下载动态图,带进度条
self.backImage.kf_setImageWithURL(NSURL(string: pictureModel!.middle_image!)!, placeholderImage: UIImage(named: "mine-icon-activity"), optionsInfo: nil, progressBlock: { (receivedSize, totalSize) -> () in
self.buttonImage.hidden = true
self.titleImage.hidden = false
//进度比例 设置进度
let progress = 1.0 * CGFloat(receivedSize) / CGFloat(totalSize)
self.progrssImage.setProgress(progress, animated: true)
//设置进度文字百分比
self.progrssImage.hidden = false
self.progrssImage.progressLabel.textColor = UIColor.redColor()
self.progrssImage.progressLabel.text = NSString(format:"%.1f%%",progress*100) as String
self.progrssImage.roundedCorners = 3;
})//图片加载完成之后的回调
{ (image, error, cacheType, imageURL) -> () in
//成功之后的回调
self.progrssImage.hidden = true
self.titleImage.hidden = true
//判断是大图
if self.pictureModel!.bigImage == 1{
// 开启图形上下文
UIGraphicsBeginImageContextWithOptions(self.backImage.size, true, 0.0);
// 将下载完的image对象绘制到图形上下文
let width = self.backImage.width;
let height = width * image!.size.height / image!.size.width
//绘制一个需要大小的尺寸,把图像画到了当前的image context里
image?.drawInRect(CGRectMake(0, 0, width, height))
// 获得图片
self.backImage.image = UIGraphicsGetImageFromCurrentImageContext()
// 结束图形上下文
UIGraphicsEndImageContext()
//显示Button
self.buttonImage.hidden = false
}else {
self.backImage.contentMode = .ScaleToFill
self.buttonImage.hidden = true;
}
}
3:字典转模型,我还是喜欢用MJExtension,相信大家都会配置Swift调用OC的桥接
4:显示百分比的进度条
我用的也是OC的DACircularProgress框架,这个框架不仅好用还很强大,简单易懂
好了,今天介绍了四个比较强大实用的框架,差不多精华模块的功能基本都能实现了,
建议cell用一个XIB描述,然后将数据的传递给cell进行赋值,