swift|swift 网络数据(controllerview)
首先导入AFNetworking文件
(1)ViewController里创建网格
import UIKit
class ViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource{
varreadID ="readID"
var flowlayout = UICollectionViewFlowLayout()
varcollection:UICollectionView?
varcollecArr=[News]()
//var collecArr = ["3","1","2"]
overridefuncviewDidLoad() {
super.viewDidLoad()
let urlStr = "http://api.jisuapi.com/news/get"
letpar : [String:Any] = [
"channel":"头条",
"appkey":"de394933e1a3e2db"
]
NetworkTools.sharedInstance.request(.GET, urlString: urlStr, parameters: par) { (result, error)in
guarderror ==nilelse{
return
}
guardletjsonDict = resultelse{
return
}
letdict = jsonDictas!NSDictionary
letresultDict = dict.value(forKey:"result")as!NSDictionary
letlistArray = resultDict.value(forKey:"list")as!NSArray
foriteminlistArray{
letdic = itemas!NSDictionary
letoneNew =News()
oneNew.title= dic.value(forKey:"title")as!String
oneNew.content= dic.value(forKey:"content")as!String
self.collecArr.append(oneNew)
}
self.collection?.reloadData()
}
// 设置网格的大小
flowlayout.itemSize=CGSize(width:self.view.frame.size.width/4, height:100)
//设置最小行间距
flowlayout.minimumLineSpacing = 1
//设置最小列间距
flowlayout.minimumInteritemSpacing = 40
//设置分区缩进量
flowlayout.sectionInset=UIEdgeInsets(top:20, left:10, bottom:20, right:10)
// 设置滚动方向
flowlayout.scrollDirection = UICollectionViewScrollDirection.vertical
// 网格对象
collection=UICollectionView(frame:CGRect(x:0, y:0, width:self.view.frame.size.width, height:self.view.frame.size.height) , collectionViewLayout:flowlayout)
// 设置代理协议
collection?.delegate=self
collection?.dataSource=self
collection?.backgroundColor = UIColor.white
collection?.register(NewsCollectionViewCell.self, forCellWithReuseIdentifier:readID)
// 添加网格
self.view.addSubview(collection!)
}
// 实现网格的协议代理
【swift|swift 网络数据(controllerview)】
funcnumberOfSections(in collectionView:UICollectionView) ->Int{
return1
}
funccollectionView(_collectionView:UICollectionView, numberOfItemsInSection section:Int) ->Int{
//return collecArr.count
returncollecArr.count
}
funccollectionView(_collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell{
// 重用cell
letcell:NewsCollectionViewCell= collectionView.dequeueReusableCell(withReuseIdentifier:readID, for: indexPath)as!NewsCollectionViewCell
cell.titlelabel?.numberOfLines=0
cell.titlelabel?.text=collecArr[indexPath.row].title
//cell.handlabel?.text = dic.content
cell.handlabel?.numberOfLines=0
returncell
}
}
(2)设置格子的格式
import UIKit
classNewsCollectionViewCell:UICollectionViewCell{
// 创建两个label
vartitlelabel:UILabel?
varhandlabel:UILabel?
overrideinit(frame:CGRect) {
super.init(frame: frame)
self.titlelabel=UILabel()
self.addSubview(titlelabel!)
self.handlabel=UILabel()
self.addSubview(handlabel!)
setTitle()
sethand()
}
funcsetTitle(){
self.titlelabel?.frame=CGRect(x:0, y:0, width:self.frame.size.width, height:20)
self.titlelabel?.font=UIFont.systemFont(ofSize:14.0)
self.titlelabel?.backgroundColor = UIColor.red
self.titlelabel?.numberOfLines=0
}
funcsethand(){
self.handlabel?.frame=CGRect(x:0, y:20, width:self.frame.size.width, height:80)
self.handlabel?.font=UIFont.systemFont(ofSize:12.0)
self.handlabel?.backgroundColor = UIColor.green
self.handlabel?.numberOfLines=0
}
requiredinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
(3) 创建model继承nsobject存类型
import UIKit
classNews:NSObject{
vartime:String=""
vartitle:String=""
varpic:String=""
varcontent:String=""
varweburl:String=""
}
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- Docker应用:容器间通信与Mariadb数据库主从复制
- 猎杀IP
- 自媒体形势分析
- 数学大作战
- 使用协程爬取网页,计算网页数据大小
- 2018.03.18
- 星期天的下午茶(一)
- Java|Java基础——数组
- Python数据分析(一)(Matplotlib使用)