首页
import UIKit
//YYModel的使用
//1.创建一个数据模型,让它遵守YYModel协议
//2.声明数据模型的属性,属性必须和对应的字典的key保持一致(属性名必须和字典的key一样;属性名必须是字典的key值)
//3.使用YYModel的方法快速将字典转换成模型;将字典数组转换成模型数组
class HotShowModel: NSObject,YYModel {
var card_type = ""
var film_id = ""
var genre = ""
var intro = ""
var large_poster_url = ""
var name = ""
var poster_url = ""
var release_time = ""
var score = ""
var score_count = ""
var wanttosee = ""
//打印模型对象所对应的值
override var description: String{
return name
}
}
import Kingfisher
class HotShowTableViewCell: UITableViewCell {
//MARK: - 属性
@IBOutlet weak var coverImageView: UIImageView!
@IBOutlet weak var soreLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var scoreCountLabel: UILabel!
//当给模型属赋值的时候,通过模型的值给子视图的属性赋值
var model:HotShowModel? = nil{
didSet{
//图片
self.coverImageView.kf_setImageWithURL(NSURL.init(string: model!.poster_url)!, placeholderImage: UIImage.init(named: "movie_default_light_760x570"))
//分数
self.soreLabel.text = model?.score
//名字
self.nameLabel.text = model?.name
//评分数量
self.scoreCountLabel.attributedText = ToolManager.mixImageAndText("home_interest", text: " \(model!.score_count)人点评")
class MainPageViewController: BasicViewController {
//MARK: - 属性
//1.滚动视图
let scrollView = UIScrollView()
//2.分段选择器
var segment:YTSegmentControl!
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBarItemSetting()
self.creatUI()
}
}
//MARK: - 界面相关
extension MainPageViewController{
override func creatUI() {
super.creatUI()
//0.关闭滚动视图自动补齐导航条高度的偏移
self.automaticallyAdjustsScrollViewInsets = false
//1.创建滚动视图
//设置frame
self.scrollView.frame = CGRectMake(0, 64, Screen_W, Screen_H-64-49)
//设置内容大小
self.scrollView.contentSize = CGSizeMake(Screen_W*2,scrollView.frame.size.height-64-49)
//设置分页
self.scrollView.pagingEnabled = true
//设置代理
self.scrollView.delegate = self
//隐藏滚动条
self.scrollView.showsHorizontalScrollIndicator = false
// self.automaticallyAdjustsScrollViewInsets = false
//显示在界面上
self.view.addSubview(self.scrollView)
//2.添加热映界面
let hotC = HotShowViewController()
self.addChildViewController(hotC)
hotC.tableView.frame = CGRectMake(0, 0,self.scrollView.frame.size.width, self.scrollView.frame.size.height)
self.scrollView.addSubview(hotC.tableView)
//3.添加预告界面
let reailerC = TrailerTableViewController()
self.addChildViewController(reailerC)
reailerC.tableView.frame = CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)
self.scrollView.addSubview(reailerC.tableView)
}
override func navigationBarItemSetting() {
super.navigationBarItemSetting()
//1.中间的item
//创建分段选择器对象
self.segment = YTSegmentControl.init(items: ["热映","预告"])
//设置frame
self.segment.frame = CGRectMake(0, 0, 100, 44)
//添加到导航条上
self.navigationItem.titleView = self.segment
//设置正常和选中的文字颜色
self.segment.titleSelectedColor = UIColor.whiteColor()
self.segment.titleColor = UIColor.whiteColor()
//设置正常状态和选中的字体
self.segment.selectedFont = UIFont.boldSystemFontOfSize(15)
self.segment.normalFont = UIFont.systemFontOfSize(12)
//设置滑块的颜色
self.segment.sliderColor = UIColor.yellowColor()
//添加事件
self.segment.addTarget(self, action: "segmentAction:")
}
}
//MARK: - scrollView Delegate
extension MainPageViewController:UIScrollViewDelegate{
//停止滚动
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
//切换分段选择器
if scrollView.contentOffset.x == 0 {
self.segment.selectedSegmentIndex = 0
return
}
self.segment.selectedSegmentIndex = 1
}
}
//MARK: - 按钮点击
extension MainPageViewController{
func segmentAction(segment:YTSegmentControl){
if segment.selectedSegmentIndex == 0 {
print("热映被选中")
self.scrollView.contentOffset = CGPointMake(0, 0)
}else{
print("预告被选中")
self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.width, 0)
}
}
}
import Alamofire
//MARK: - 属性
class HotShowViewController: UITableViewController{
//1.数据源数组
//NSMutableArray == [AnyObject]
lazy var dataArray:NSMutableArray = {
return NSMutableArray()
}()
}
//MARK: - 生命周期
extension HotShowViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.getNetData(1)
self.creatUI()
self.addRefreshView()
}
}
//MARK: - 添加刷新控件
extension HotShowViewController{
func addRefreshView() {
//1.添加header
self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
self.getNetData(1)
})
//2.添加footer
self.tableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
self.getNetData(self.dataArray.count/12+1)
})
}
}
//MARK: - 界面相关
extension HotShowViewController{
func creatUI() {
//1.注册cell
self.tableView.registerNib(UINib.init(nibName: "HotShowTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
//2.设置cell高度
self.tableView.rowHeight = 200
//3.去掉分割线
self.tableView.separatorStyle = .None
}
}
//MARK: - tableView 协议方法
extension HotShowViewController{
//1.cell个数
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArray.count
}
//2.创建cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//a.创建cell
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! HotShowTableViewCell
//b.刷新数据
cell.model = self.dataArray[indexPath.row] as? HotShowModel
//c.返回cell
return cell
}
}
//MARK: - 网络请求
extension HotShowViewController{
func getNetData(page:Int){
//开始请求数据
KVNProgress.showWithStatus("正在加载")
//注意:page是从1开始
Alamofire.request(.GET, Url_HotShow, parameters: ["count":12,"page":page], encoding: .URL, headers: nil).responseJSON(options: .MutableContainers) { (data) in
if let json = data.result.value{
//提示请求成功
KVNProgress.showSuccessWithStatus("加载成功")
//0.判断是否是刷新产生的数据请求
if self.tableView.mj_header.isRefreshing(){
self.dataArray.removeAllObjects()
}
//1.获取存放字典的数组
let dataDict = json.objectForKey("data") as! NSDictionary
let ranklisthotArray = dataDict["ranklist_hot"]
//2.使用YYModel将字典数组转换成模型数组
//参数1:模型类型
//参数2:需要转换的数组对象
let modelArray = NSArray.yy_modelArrayWithClass(HotShowModel.self, json: ranklisthotArray)
//3.将数组存到数据源数组中
self.dataArray.addObjectsFromArray(modelArray)
//让刷新控件停止刷新
self.tableView.mj_header.endRefreshing()
self.tableView.mj_footer.endRefreshing()
//4.刷新tableView
self.tableView.reloadData()
}else{
KVNProgress.showErrorWithStatus("请求失败")
}
}//Alamofire结束
}//函数结束
}
search
class SearchViewController: YTViewController{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBarItemSetting()
}
}
//MARK: - 界面相关
extension SearchViewController{
override func navigationBarItemSetting() {
super.navigationBarItemSetting()
//1.返回按钮
let returnItem = UIBarButtonItem.init(image: UIImage.init(named: "common_arrow_back_dark"), style: .Done, target: self, action: "backItemAction")
self.navigationItem.leftBarButtonItem = returnItem
//2.搜索框
//创建搜索框对象
let searchBar = UISearchBar.init(frame: CGRectMake(0, 0, 200, 40))
//显示删除按钮
searchBar.showsCancelButton = true
//设置占位文字
searchBar.placeholder = "电影,演员"
//设置代理
searchBar.delegate = self
//将搜索框添加到导航条上
self.navigationItem.titleView = searchBar
}
}
//MARK: - searchBar Delegate
extension SearchViewController:UISearchBarDelegate{
//2.cancel按钮被点击
func searchBarCancelButtonClicked(searchBar: UISearchBar){
searchBar.resignFirstResponder()
}
//1.搜索按钮被点击
func searchBarSearchButtonClicked(searchBar: UISearchBar){
//1.开始搜索
//2.收起键盘
searchBar.resignFirstResponder()
}
}
//MARK: - 按钮点击
extension SearchViewController{
func backItemAction() {
self.navigationController?.popViewControllerAnimated(true)
}
}