//
// HomeViewController.swift
// LoveLife-Swift
//
// Created by 杨阳 on 16/7/11.
// Copyright © 2016年 yangyang. All rights reserved.
//
import UIKit
class HomeViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
//声明属性的同时将属性进行初始化,可以避免封包和拆包的问题
//var tableView: UITableView?
var tableView = UITableView()
var hud = MBProgressHUD()
var page = 0
var dataArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
//创建UI
createUI()
//创建刷新
createRefresh()
}
//MARK: - 创建刷新
func createRefresh() {
//下拉刷新
self.tableView.header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: "loadNewData")
//上拉加载
self.tableView.footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: "loadMoreData")
//第一次进入页面的时候默认刷新一次
self.tableView.header.beginRefreshing()
}
//MARK: - 上下拉刷新
func loadNewData() {
self.page = 1
self.dataArray = NSMutableArray(capacity: 0)
loadData()
}
func loadMoreData() {
self.page++
loadData()
}
func loadData() {
//启动活动指示器
self.hud.show(true)
//获取请求对象
let manager = AFHTTPSessionManager()
manager.GET("http://open4.bantangapp.com/recommend/index?client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&page=\(self.page)&pagesize=20", parameters: nil, success: { (dataTask: NSURLSessionDataTask?, responseObject: AnyObject?) -> Void in
print(responseObject)
//数据解析
let dic = responseObject?.objectForKey("data")
let array = dic?.objectForKey("entry_list")
for i in 0..<array!.count {
let dic = array![i]
//创建模型
let model = HomeModel()
//AFNetWorking-->Alamofire
//给属性进行赋值.在Swift里面进行数据解析的 第三方库叫SwiftyModel。作用跟OC中JSONModel一样
//as?和as!都表示强制解析或者强制转换,as?表示需要强制解析的值可为空,如果需要强制解析的值为空的话,你还使用了as!,直接崩溃
model.pic1 = dic["pic1"] as? String
model.title = dic["title"] as? String
self.dataArray.addObject(model)
}
//拿到数据之后停止活动指示器,停止刷新,去刷新tableView
self.hud.hide(true)
if self.page == 1 {
self.tableView.header.endRefreshing()
} else {
self.tableView.footer.endRefreshing()
}
self.tableView.reloadData()
}) { (dataTask: NSURLSessionDataTask?, error: ErrorType?) -> Void in
print("错误原因是\(error)")
}
}
//MARK: - 创建UI
func createUI() {
//创建tableView
self.tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
//设置代理
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
//创建轮播
let cyclePlaying = Carousel(frame: CGRectMake(0,0,screenWidth,screenHeight / 3))
//设置是否需要pageCOntrol
cyclePlaying.needPageControl = true
//设置是否需要无限轮播
cyclePlaying.infiniteLoop = true
//设置pageCOntrol的位置
cyclePlaying.pageControlPositionType = PAGE_CONTROL_POSITION_TYPE_MIDDLE
//设置需要显示图片
cyclePlaying.imageArray = ["shili8","shili10","shili13","shili15","shili2"]
//设置为tableView的tableHeaderView
self.tableView.tableHeaderView = cyclePlaying
//活动指示器
self.hud = MBProgressHUD(view: self.view)
//设置加载的文字
self.hud.labelText = "正在加载..."
//设置菊花的颜色
self.hud.activityIndicatorColor = UIColor.whiteColor()
//设置背景颜色
self.hud.color = UIColor(white: 0, alpha: 0.5)
self.view.addSubview(self.hud)
//注册cell
let nib = UINib(nibName: "HomeCell", bundle: nil)
self.tableView.registerNib(nib, forCellReuseIdentifier: "cellID")
}
//MARK: - 实现代理方法
//确定row的个数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArray.count
}
//创建cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellID", forIndexPath: indexPath) as! HomeCell
//赋值
cell.model = self.dataArray.objectAtIndex(indexPath.row) as? HomeModel
return cell
}
//确定cell的高复
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 200
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}