1、视图
import UIKit
import Alamofire
import ObjectMapper
import MJRefresh
class NewCollectionView: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
var dataArrays = NSMutableArray()
var newCollecView:UICollectionView?
var pageNo:Int = 1
func createCollectionView() {
let flayout = UICollectionViewFlowLayout()
//设置item尺寸
flayout.itemSize = CGSize(width: (ScreenWidth-18)/2, height: 200)
//设置左右间隔
// flayout.minimumInteritemSpacing = 6
// //设置上下间隔
// flayout.minimumLineSpacing = 6
// flayout.sectionInset = UIEdgeInsetsMake(6, 6, 6, 6)
//设置滚动方向
flayout.scrollDirection = .vertical
//初始化
let collecView = UICollectionView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: self.frame.size.height), collectionViewLayout: flayout)
self.addSubview(collecView)
collecView.delegate = self
collecView.dataSource = self
collecView.backgroundColor = #colorLiteral(red: 0.3098039329, green: 0.2039215714, blue: 0.03921568766, alpha: 1)
// collecView.register(NewCell.self, forCellWithReuseIdentifier: "OtherCell")
collecView.register(UINib.init(nibName: "OtherCell", bundle: nil), forCellWithReuseIdentifier: "OtherCell")
collecView.alwaysBounceVertical = true;
self.newCollecView = collecView
let header:MJRefreshGifHeader = MJRefreshGifHeader(refreshingTarget: self, refreshingAction: #selector(updateLoadData))
//设置mj_header
self.newCollecView?.mj_header = header
//定义动画刷新footer
let footer:MJRefreshAutoStateFooter = MJRefreshAutoGifFooter(refreshingTarget: self, refreshingAction: #selector(footRefreshAction))
// let footer:MJRefreshAutoFooter = MJRefreshAutoFooter(refreshingTarget: self, refreshingAction: #selector(footRefreshAction))
//设置mj_header
self.newCollecView?.mj_footer = footer
}
func updateLoadData() {
print("刷新")
self.newCollecView?.mj_header.endRefreshing()
self.pageNo = 1
self.requestHomeData()
}
func footRefreshAction() {
self.pageNo += 1
self.requestHomeData()
}
//数据解析
func requestHomeData(){
let urls = "http://zhibo.4g.jxnews.com.cn/v3/Activity/getADVList?uid=27872683&username=b0NmTnp2eVowdUItZm53WC1OSkNGMHduZ3Brbw==&uuid=3CD5D574-A990-443A-BB08-8171BF5341AB&city=5LiK5rW35biC&plateform=1&version=2.2.1&platform=1&isnight=0&page=\(self.pageNo)&pageSize=20"
Alamofire.request(urls, method: .get, parameters: nil, encoding: JSONEncoding.default).responseJSON { (response) in
print(response)
self.newCollecView?.mj_header.endRefreshing()
self.newCollecView?.mj_footer.endRefreshing()
switch response.result.isSuccess{
case true:
let dicData = response.result.value as! NSDictionary
let array = dicData["data"] as! NSArray
let nsArray:NSMutableArray=NSMutableArray()
for model in array{
let statues:MainHomeModel = Mapper<MainHomeModel>().map(JSON: model as! [String : Any])!
nsArray.add(statues)
}
if self.pageNo>1 {
self.dataArrays.addObjects(from: nsArray as! [Any])
}else{
self.dataArrays.removeAllObjects()
self.dataArrays.addObjects(from: nsArray as! [Any])
}
self.newCollecView?.reloadData()
case false:
print("首页数据错误\(String(describing: response.result.error))")
}
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dataArrays.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Xib的cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OtherCell", for: indexPath) as! OtherCell
if self.dataArrays.count>0 {
let model:MainHomeModel = self.dataArrays[indexPath.row] as! MainHomeModel
cell.titleLabels.text = model.title
cell.bigImage.sd_setImage(with: URL(string: model.showImage!), placeholderImage: UIImage.init(named: "02"))
}
// 代码编写Cell
// let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OtherCell", for: indexPath) as! NewCell
// let model:MainHomeModel = self.dataArrays[indexPath.row] as! MainHomeModel
// cell.titleLabels.text = model.title
// cell.bottImageView.sd_setImage(with: URL(string: model.showImage!))
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.item)
}
}
2、model数据源
//
// MainHomeModel.swift
// UICollectionView
//
// Created by modai on 2017/7/28.
// Copyright © 2017年 huanqiu. All rights reserved.
//
import UIKit
import ObjectMapper
class MainHomeModel: Mappable {
var id:String?
var platform:String?
var joinNum:String?
var showImage:String?
var title:String?
init() {
}
required init?(map:Map) {
mapping(map: map)
}
func mapping(map: Map) {
platform <- map["platform"]
id <- map["id"]
joinNum <- map["joinNum"]
showImage <- map["showImage"]
title <- map["title"]
}
}
3、xib自定义Cell
import UIKit
class OtherCell: UICollectionViewCell {
@IBOutlet weak var titleLabels: UILabel!
@IBOutlet weak var bigImage: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}