// 一个
import UIKit
class SPViewController: UIViewController ,UICollectionViewDelegate,UICollectionViewDataSource{
var readID = "readID"
var flowlayout = UICollectionViewFlowLayout()
var collection:UICollectionView?
var collArr:[Model]?
override func viewDidLoad() {
super.viewDidLoad()
// 设置网格item 的大小
flowlayout.itemSize = CGSize(width: (self.view.frame.size.width/2)-10, height: 300)
// 设置最小行间距
flowlayout.minimumLineSpacing = 1
// 设置最小列间距
flowlayout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 20, right: 5)
// 设置滚动方向
flowlayout.scrollDirection = UICollectionViewScrollDirection.vertical
// 创建网格
collection = UICollectionView(frame: self.view.frame, collectionViewLayout: flowlayout)
// 设置网格的代理
collection?.delegate = self
collection?.dataSource = self
// 注册
collection?.register(OwnCollectionViewCell.self, forCellWithReuseIdentifier: readID)
// 添加到主视图
self.view.addSubview(collection!)
}
// MARK:program mark ============ 实现网格的代理协议 ====================
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collArr != nil {
return collArr!.count
}
else{
return 10
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// 重用cell
let cell:OwnCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: readID, for: indexPath) as! OwnCollectionViewCell
// 判断数据是否为空
if collArr != nil {
let dic = self.collArr![indexPath.item]
// 导入网络图片
let str = dic.imageUrl
let url = NSURL(string: str)
cell.imageS?.sd_setImage(with: url as! URL, completed: nil)
}
else{
cell.imageS?.image = UIImage(named: "Snip20180825_21")
cell.titleLable?.text = "ASMR耳廓芦荟按摩专业舒缓按摩新曲放松快来收听很好听的"
}
return cell
}
// 视图只要加载就会显示
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 调用请求数据方法
self.requestData()
}
// 请求数据
func requestData() -> Void {
let Net = NetWork.Networking.get(urlString: "testVideo", parameters: nil, success: { (data) in
self.collArr = (data as? [Model])!
// 刷新网格
DispatchQueue.main.async {
self.collection?.reloadData()
}
}) { (error) in
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
(2)
class NetWork: NSObject {
static var Nearr:[Model] = []
// 使用afnetworking调用数据
class Networking: AFHTTPSessionManager {
static let shareinstance:Networking = {
let strUrl = NSURL(string: "http://123.126.40.109:9002/showdoc/index.php?s=/Home/Item/show/item_id/2")
let manager = Networking.init(baseURL: strUrl as! URL , sessionConfiguration: URLSessionConfiguration.default)
return manager
}()
class func get(urlString:String,parameters:AnyObject?,success:((_ responseObject:AnyObject?) -> Void)?,failure:((_ error:NSError) -> Void)?) -> Void {
Networking.shareinstance.get(urlString, parameters: parameters, progress: { (progress) in }, success: { (task, responseObject) in
//如果responseObject不为空时
if responseObject != nil {
let result = responseObject as! Array<Any>
var arr:[Model] = []
for item in result{
let dic = (item as! NSDictionary)
let newNet = Model()
newNet.imageUrl = dic .value(forKey: "imageUrl") as! String
arr .append(newNet)
}
}
}, failure: { (task, error) in
failure!(error as NSError)
})
(3)
import UIKit
class OwnCollectionViewCell: UICollectionViewCell {
var imageS:UIImageView?
var titleLable:UILabel?
override init(frame: CGRect) {
super .init(frame: frame)
self.imageS = UIImageView()
self.addSubview(imageS!)
self.titleLable = UILabel()
self.addSubview(titleLable!)
Setimage()
Setlabel()
}
// 设置图片的额位置
func Setimage() {
imageS?.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height - 50)
imageS?.backgroundColor = UIColor.red
}
// 设置标题的位置
func Setlabel() {
titleLable?.frame = CGRect(x: 0, y: self.frame.size.height - 50, width: self.frame.size.width, height: 50)
titleLable?.font = UIFont .systemFont(ofSize: 16.0)
titleLable?.numberOfLines = 0
titleLable?.backgroundColor = UIColor .black
titleLable?.textColor = UIColor.white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}