PageMenu分页控制器(进阶篇)-不同分页页面的实现
2019-01-21 本文已影响0人
瑟闻风倾
1. 界面展示
菜单分页页面1.png菜单分页页面2.png
2. 界面设计
界面设计.png3.具体实现
(1) BillPageMenuViewController.swift
//
// BillPageMenuViewController.swift
// JackUChat
//
// Created by 徐云 on 2019/1/15.
// Copyright © 2019 Liy. All rights reserved.
//
import UIKit
import PageMenu
class BillPageMenuViewController: UIViewController,CAPSPageMenuDelegate {
var pageMenu : CAPSPageMenu?
let instalmentMenuArray = ["分期机器","近期还款"]
var controllerArray : [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
showMenu()
}
func showMenu() {
//设置菜单控制器
setPageMenu()
//设置菜单样式和位置
setPageMenuStyleAndLocation()
//添加到当前控制器的视图上
self.view.addSubview(self.pageMenu!.view)
//代理设置
pageMenu!.delegate = self
}
// MARK: - 设置菜单控制器
func setPageMenu() {
// for category in instalmentMenuArray {
// let vc = self.storyboard?.instantiateViewController(withIdentifier: "INSTALMENT_VC_ID") as! InstalmentViewController
// //print(category)
// vc.title = category//传值:设置控制器的title值
// self.controllerArray.append(vc)
//
// }
let controller1 = self.storyboard?.instantiateViewController(withIdentifier: "INSTALMENT_TVC_ID") as! InstalmentTableViewController
controller1.title = "分期机器"
self.controllerArray.append(controller1)
let controller2 = self.storyboard?.instantiateViewController(withIdentifier: "REPAYMENT_TVC_ID") as! RepaymentTableViewController
controller2.title = "近期还款"
self.controllerArray.append(controller2)
}
// MARK: - 设置菜单样式和位置
func setPageMenuStyleAndLocation() {
//设置菜单样式
let parameters: [CAPSPageMenuOption] = [
.menuItemSeparatorWidth(4.3),
.scrollMenuBackgroundColor(UIColor.white),
.viewBackgroundColor(UIColor.orange),
.bottomMenuHairlineColor(UIColor.gray),
.selectionIndicatorColor(UIColor.jackColor),
.menuMargin(20.0),
.menuHeight(40.0),
.selectedMenuItemLabelColor(UIColor.jackColor),
.unselectedMenuItemLabelColor(UIColor.gray),
.menuItemFont(UIFont(name: "HelveticaNeue-Medium", size: 14.0)!),
.useMenuLikeSegmentedControl(true),
.menuItemSeparatorRoundEdges(true),
.selectionIndicatorHeight(2.0),
.menuItemSeparatorPercentageHeight(0.1)
]
//设置菜单位置
let frame = CGRect(x: 0, y: 100, width: self.view.frame.width, height: self.view.frame.height)
pageMenu = CAPSPageMenu(viewControllers: self.controllerArray, frame: frame, pageMenuOptions: parameters)
//print(self.view.frame.height)
}
// MARK: - 代理方法
func didMoveToPage(_ controller: UIViewController, index: Int) {
print("BillPageMenuViewController******PageView页面顶部滑动菜单索引值:" + index.description)
// if (index == 0) {
// defaults.set(0, forKey: "menu_title_index")
// }else if (index == 1) {
// defaults.set(1, forKey: "menu_title_index")
// }else if (index == 2) {
// defaults.set(3, forKey: "menu_title_index")
// }
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
备注
:"INSTALMENT_TVC_ID"为视图InstalmentTableViewController的StroyBoard ID;"REPAYMENT_TVC_ID"为视图RepaymentTableViewController的StroyBoard ID。
(2) InstalmentTableViewController.swift
//
// InstalmentTableViewController.swift
// JackUChat
//
// Created by 徐云 on 2019/1/18.
// Copyright © 2019 Liy. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
class InstalmentTableViewController: UITableViewController {
@IBOutlet weak var timeLabel: UIButton!
@IBOutlet weak var searchBtn: UIButton!
var devices:[Device] = []
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
print("InstalmentTableViewController***PageView页面传递的当前页面title值:" + title!)
getListByAlomafire()
}
// MARK: - Table view data source
// override func numberOfSections(in tableView: UITableView) -> Int {
// // #warning Incomplete implementation, return the number of sections
// return 0
// }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return devices.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
let cellId = String(describing: OrderCell.self)
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! OrderCell
let device = devices[indexPath.row]
cell.deviceNameLabel.text = device.deviceName
cell.deviceNoLabel.text = device.deviceCount + "期,共" + device.deviceId + "元"
if device.deviceStatus == "0" {
cell.countLabel.text = "已完成"
cell.countLabel.textColor = UIColor.gray
}else if device.deviceStatus == "1" {
cell.countLabel.text = "逾期"
cell.countLabel.textColor = UIColor.red
}
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
func getListByAlomafire() {
var tab = "machine"
let data = Date()
let timeFormatter1 = DateFormatter()
timeFormatter1.dateFormat = "yyyy"
let currentYear = timeFormatter1.string(from: data)
let timeFormatter2 = DateFormatter()
timeFormatter2.dateFormat = "MM"
let currentMonth = timeFormatter2.string(from: data)
let params:Parameters = ["t":tab,"year":currentYear,"month":currentMonth,"page":"1"]
AlamofireHelper.shareInstance.requestData(.post, url: "staging/index", parameters: params) { (result) in
let jsonDictory = JSON(result as Any)
let code = jsonDictory["code"].string
let msg = jsonDictory["msg"].string
if(code == "0"){
print("成功:"+code!+","+msg!)
let list = jsonDictory["data"]["list"].array
for ele in list!{
let device = Device(deviceId: ele["order_total"].string ?? "", deviceName: ele["machine"]["name"].string ?? "", deviceStatus: ele["is_overdue"].string ?? "", deviceCount: ele["count"].string ?? "", deviceImage: "", date: "")
self.devices.append(device)
}
//dump(self.devices)//打印
//异步获取数据,需在主线程中更新
OperationQueue.main.addOperation {
self.tableView.reloadData()
self.tableView.refreshControl?.endRefreshing()//加载完数据后停止下拉刷新动画
}
}else{
print("失败")
}
}
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
}
备注
:获取的网络数据示例
{
"code": "0",
"msg": "success",
"data": {
"list": [
{
"company_id": "50",
"cm_id": "415",
"order_status": "0",
"del_time": "0",
"machine": {
"name": "老化测试04",
"sn": "LHCS04",
"machine_id": "29",
"cate": "2",
"cate2": "0",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/public/images/nopic300.png",
"tags": null,
"protocol": null,
"location": "",
"device_id": "39464240",
"imsi": "460040148224171",
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "26475",
"ci": "12266",
"csq": "0",
"rssi": null,
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1534829481",
"rent_out": "1",
"status": "0",
"status_time": "1546007261",
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": null,
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"company": {
"company": "经销商测试专用工厂"
},
"i": 1,
"machine_id": "29",
"cate": "0",
"cust_group": "0",
"is_rent": "0",
"price": "0.000",
"is_min": "0",
"min_num": "0",
"order_total": "0.03",
"order_money": "0.00",
"order_num": "3",
"is_overdue": "0",
"bg_time": "1547704807",
"end_time": "1547711047",
"is_lock": "0",
"force_lock": "0",
"status": "1",
"add_time": "1547704807",
"is_online": "0",
"con_type": "0",
"sale_type": "2",
"parent_id": "0",
"origin": null,
"order": [],
"count": "3",
"use_time": 0
},
{
"cm_id": "414",
"company_id": "50",
"machine_id": "33",
"cate": "0",
"cust_group": "0",
"is_rent": "0",
"price": "0.000",
"is_min": "0",
"min_num": "0",
"order_total": "3.00",
"order_money": "0.00",
"order_num": "3",
"order_status": "0",
"is_overdue": "0",
"bg_time": "1547704807",
"end_time": "1547712892",
"del_time": "0",
"is_lock": "0",
"force_lock": "0",
"status": "1",
"add_time": "1547704807",
"is_online": "0",
"con_type": "0",
"sale_type": "2",
"parent_id": "0",
"origin": null,
"machine": {
"machine_id": "33",
"cate": "2",
"cate2": "0",
"name": "迈卡袖衩机",
"sn": "0109",
"channel": "1",
"price": "0.300",
"description": null,
"image": "http://s.uchat.com.cn/public/images/nopic300.png",
"tags": null,
"protocol": null,
"location": "",
"device_id": "39597199",
"imsi": null,
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "0",
"ci": "0",
"csq": "0",
"rssi": null,
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1534917235",
"rent_out": "1",
"status": "0",
"status_time": null,
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": "{\"\":null}",
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"company": {
"company": "经销商测试专用工厂"
},
"order": [],
"count": "3",
"use_time": 0,
"i": 2
},
{
"cm_id": "346",
"company_id": "50",
"machine_id": "13",
"cate": {
"cate_id": "104",
"sort": "0",
"parent_id": "2",
"cate_name": "1005折叠压烫机",
"image": "/upload/image/2018/09/ff168158835c5fa6fbd4cdd484578c8e.jpg",
"machine_num": "0",
"unit": "个",
"time_unit": "秒",
"is_jtj": "0",
"tiered_num": "0",
"tiered_discount": "0.00"
},
"cust_group": "0",
"is_rent": "0",
"price": "0.000",
"is_min": "0",
"min_num": "0",
"order_total": "900.00",
"order_money": "0.00",
"order_num": "3",
"order_status": "0",
"is_overdue": "0",
"bg_time": "1546940005",
"end_time": "1547544805",
"del_time": "0",
"is_lock": "0",
"force_lock": "0",
"status": "1",
"add_time": "1546940005",
"is_online": "0",
"con_type": "1",
"sale_type": "2",
"parent_id": "343",
"origin": null,
"machine": {
"machine_id": "13",
"cate": "2",
"cate2": "104",
"name": "折叠压烫机",
"sn": "10052017679",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/upload/image/2018/09/ff168158835c5fa6fbd4cdd484578c8e_150x150.jpg",
"tags": null,
"protocol": null,
"location": "",
"device_id": "36488007",
"imsi": null,
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "0",
"ci": "0",
"csq": "0",
"rssi": null,
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1532401924",
"rent_out": "1",
"status": "0",
"status_time": "1544075712",
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": null,
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"company": {
"company": "经销商测试专用工厂"
},
"order": [],
"count": "3",
"use_time": 0,
"i": 3
},
{
"cm_id": "343",
"company_id": "50",
"machine_id": "234",
"cate": "0",
"cust_group": "0",
"is_rent": "0",
"price": "0.000",
"is_min": "0",
"min_num": "0",
"order_total": "1.00",
"order_money": "1.00",
"order_num": "1",
"order_status": "1",
"is_overdue": "1",
"bg_time": "1546876800",
"end_time": "1578412800",
"del_time": "0",
"is_lock": "0",
"force_lock": "0",
"status": "1",
"add_time": "1546937955",
"is_online": "0",
"con_type": "0",
"sale_type": "2",
"parent_id": "0",
"origin": null,
"machine": {
"machine_id": "234",
"cate": "60",
"cate2": "0",
"name": "经销商测试工厂中继",
"sn": "agentrelay",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/public/images/nopic300.png",
"tags": null,
"protocol": null,
"location": "",
"device_id": "514180722",
"imsi": null,
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "0",
"ci": "0",
"csq": "0",
"rssi": null,
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1546871622",
"rent_out": "1",
"status": "0",
"status_time": null,
"login_type": "7",
"maintain_time": "1000",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": null,
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"company": {
"company": "经销商测试专用工厂"
},
"order": [
{
"order_id": "8",
"app": "agent",
"sort": "1",
"user_id": "130",
"agent_user_id": "151",
"company_id": "50",
"cm_id": "343",
"order_no": null,
"amount": "1.00",
"total": "1.00",
"order_status": "-1",
"del_time": "1547631708",
"repay_time": "2019-01-08",
"ip": "183.129.132.34",
"ip_addr": "中国 浙江 杭州 ",
"remark": "{\"pay_amount\":\"1\",\"repay\":[{\"money\":\"1.00\",\"order_id\":\"8\"}]}",
"pay_type": "1",
"pay_way": "cash",
"pay_id": null,
"pay_status": "0",
"create_time": "2019-01-08 16:59:15",
"machine": {
"name": "经销商测试工厂中继",
"sn": "agentrelay",
"machine_id": "234"
},
"company": {
"company": "经销商测试专用工厂"
},
"i": 1
}
],
"count": "1",
"machineNum": "1",
"use_time": 0,
"i": 4
}
]
}
}
(3) RepaymentTableViewController.swift
//
// RepaymentTableViewController.swift
// JackUChat
//
// Created by 徐云 on 2019/1/21.
// Copyright © 2019 Liy. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
class RepaymentTableViewController: UITableViewController {
@IBOutlet weak var timeLabel: UIButton!
@IBOutlet weak var searchBtn: UIButton!
var devices:[Device] = []
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
print("RepaymentTableViewController***PageView页面传递的当前页面title值:" + title!)
getListByAlomafire()
}
// MARK: - Table view data source
// override func numberOfSections(in tableView: UITableView) -> Int {
// // #warning Incomplete implementation, return the number of sections
// return 0
// }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return devices.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
let cellId = String(describing: RepaymentCell.self)
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! RepaymentCell
let device = devices[indexPath.row]
cell.firstLabel.text = "第" + device.deviceImage + "期: " + device.deviceCount + "元"
cell.secondLabel.text = "客户名称: " + device.deviceId
cell.thirdLabel.text = "机器名称: " + device.deviceName
cell.fourthLabel.text = "截止日期: " + device.date
if device.deviceStatus == "-1" {
cell.fifthLabel.text = "逾期"
cell.fifthLabel.textColor = UIColor.red
}else if device.deviceStatus == "0" {
cell.fifthLabel.text = "已完成"
cell.fifthLabel.textColor = UIColor.gray
}else if device.deviceStatus == "1" {
cell.fifthLabel.text = "未完成"
cell.fifthLabel.textColor = UIColor.yellow
}
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
func getListByAlomafire() {
var tab = "time"
let data = Date()
let timeFormatter1 = DateFormatter()
timeFormatter1.dateFormat = "yyyy"
let currentYear = timeFormatter1.string(from: data)
let timeFormatter2 = DateFormatter()
timeFormatter2.dateFormat = "MM"
let currentMonth = timeFormatter2.string(from: data)
let params:Parameters = ["t":tab,"year":currentYear,"month":currentMonth,"page":"1"]
AlamofireHelper.shareInstance.requestData(.post, url: "staging/index", parameters: params) { (result) in
let jsonDictory = JSON(result as Any)
let code = jsonDictory["code"].string
let msg = jsonDictory["msg"].string
if(code == "0"){
print("成功:"+code!+","+msg!)
let list = jsonDictory["data"]["list"].array
for ele in list!{
let device = Device(deviceId: ele["company"]["company"].string ?? "", deviceName: ele["machine"]["name"].string ?? "", deviceStatus: ele["order_status"].string ?? "", deviceCount: ele["total"].string ?? "", deviceImage: ele["sort"].string ?? "", date: ele["repay_time"].string ?? "")
self.devices.append(device)
}
dump(self.devices)//打印
//异步获取数据,需在主线程中更新
OperationQueue.main.addOperation {
self.tableView.reloadData()
self.tableView.refreshControl?.endRefreshing()//加载完数据后停止下拉刷新动画
}
}else{
print("失败")
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
备注
:获取的网络数据示例
{
"code": "0",
"msg": "success",
"data": {
"list": [
{
"order_id": "8",
"app": "agent",
"sort": "1",
"user_id": "130",
"agent_user_id": "151",
"company_id": "50",
"cm_id": "343",
"order_no": null,
"amount": "1.00",
"total": "1.00",
"order_status": "-1",
"del_time": "1547631708",
"repay_time": "2019-01-08",
"ip": "183.129.132.34",
"ip_addr": "中国 浙江 杭州 ",
"remark": "{\"pay_amount\":\"1\",\"repay\":[{\"money\":\"1.00\",\"order_id\":\"8\"}]}",
"pay_type": "1",
"pay_way": "cash",
"pay_id": null,
"pay_status": "0",
"create_time": "2019-01-08 16:59:15",
"machine": {
"name": "经销商测试工厂中继",
"sn": "agentrelay",
"machine_id": "234"
},
"company": {
"company": "经销商测试专用工厂"
},
"i": 1
}
]
}
}