import UIKit
import SVProgressHUD
class bourseViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,StarsSortTableViewDelegate,StarsScreenViewDelegate {
var cells = ["默认排序", "按价格排序", "按代数排序", "按生命排序", "按品质排序", "按等级排序", "按文明排序", "按探索排序"]
/// 排序方式
var sorts = ["","price","parent_id","life","quality","level","culture","ability"]
var ordercells = ["默认排序", "顺序", "倒叙"]
/// 排序方式
var ordersorts = ["","ASC","DESC"]
@IBOutlet var segmentcontrol: UISegmentedControl!
@IBOutlet var leftableview: UITableView!
@IBOutlet var rightableview: UITableView!
// 底部加载
let footer = MJRefreshAutoNormalFooter()
var items = [StarsHomeItem]()
var type = Int()
var order = String()
var sortnum = String()
var clicknum = 0
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.topViewController?.title = "交易所"
self.view.backgroundColor = UIColor.white
setupSegment()
setupTableView()
loadData()
setupTitlesView()
//上刷新相关设置
footer.setRefreshingTarget(self, refreshingAction: #selector(bourseViewController.footerLoad))
//是否自动加载(默认为true,即表格滑到底部就自动加载)
footer.isAutomaticallyRefresh = false
self.leftableview!.mj_footer = footer
self.rightableview!.mj_footer = footer
}
//分段按钮
func setupSegment()
{
let itemtitles=["购买星球","我的挂单"]
segmentcontrol = UISegmentedControl.init(items: itemtitles)
segmentcontrol.frame = CGRect(x: 46, y: 65, width: 322, height: 35)
segmentcontrol.selectedSegmentIndex = 0 //默认选中第1项
type = 0
segmentcontrol.addTarget(self, action: #selector(bourseViewController.segmentDidchange(_:)),
for: .valueChanged) //添加值改变监听
self.view.addSubview(segmentcontrol!)
}
@objc func segmentDidchange(_ segmented:UISegmentedControl){
//获得选项的索引
// print(segmented.selectedSegmentIndex)
switch (segmented.selectedSegmentIndex)
{
case 0:
leftableview.isHidden = false
rightableview.isHidden = true
type = 0
break
case 1:
leftableview.isHidden = true
rightableview.isHidden = false
type = 1
break
default:
break
}
}
/// 顶部标签栏
func setupTitlesView() {
// 顶部背景
let bgView = UIView()
bgView.backgroundColor = StarsGlobalColor()
bgView.frame = CGRect(x: 0, y: kTitlesViewY + 35, width: SCREENW, height: kTitlesViewH)
view.addSubview(bgView)
//排序
let sortLabel = UILabel()
sortLabel.text = "排序:"
sortLabel.textAlignment = .center
sortLabel.frame = CGRect(x: 20, y: 0, width: 42, height: kTitlesViewH)
sortLabel.textColor = StarsColor(r: 20, g: 6, b: 16, a: 1.0)
bgView.addSubview(sortLabel)
// 选择按钮
bgView.addSubview(arrowButton)
arrowButton.snp.makeConstraints { (make) in
make.size.equalTo(CGSize(width: 102, height: kTitlesViewH))
make.left.equalTo(55)
make.top.equalTo(0)
}
//顺序
let rankLabel = UILabel()
rankLabel.text = "排序:"
rankLabel.textAlignment = .center
rankLabel.frame = CGRect(x: 180, y: 0, width: 42, height: kTitlesViewH)
rankLabel.textColor = StarsColor(r: 20, g: 6, b: 16, a: 1.0)
bgView.addSubview(rankLabel)
// 选择按钮
bgView.addSubview(orderButton)
orderButton.snp.makeConstraints { (make) in
make.size.equalTo(CGSize(width: 102, height: kTitlesViewH))
make.left.equalTo(210)
make.top.equalTo(0)
}
//筛选按钮
bgView.addSubview(screenButton)
screenButton.snp.makeConstraints { (make) in
make.size.equalTo(CGSize(width: 95, height: kTitlesViewH))
make.right.equalTo(0)
make.top.equalTo(0)
}
}
// MARK: - YMSortTableViewDelegate
func sortView(sortView: bourseSortTableView, choicenum: Int) {
/// 根据搜索条件进行搜索//排序字段(price,parent_id,life,quality,level,culture,ability)
// "sort":sort, //排序,正序为ASC倒序为DESC
if clicknum == 1{
order = sorts[choicenum]
arrowButton.setTitle(cells[choicenum], for: .normal)
}else{
sortnum = ordersorts[choicenum]
orderButton.setTitle(ordercells[choicenum], for: .normal)
}
print("order",order,sortnum)
//加载数据
weak var weakSelf = self
NetWorkTool.shareNetworkTool.loadHomeInfo(status: "", cointype: "", order: order, sort: sortnum, type: "", page: boursepagenum) { (homeItems) in
weakSelf!.items = homeItems
print("dismiss")
sortView.dismiss()
weakSelf?.leftableview.reloadData()
}
}
func screentView(screenView: bourseScreenView, Starstype: String, Statetype: String, Cointype: String) {
// "type":type, //默认所有星球,初代星球=1,幻想=2
// "status":status, //待交易=2,待博弈=3,勾选其他传空,默认不勾选传空
// "coin_type":cointype, //1-eth,2-sts,默认不选传空
//加载数据
weak var weakSelf = self
NetWorkTool.shareNetworkTool.loadHomeInfo(status: Statetype, cointype: Cointype, order: order, sort: sortnum, type: Starstype, page: boursepagenum) { (homeItems) in
weakSelf!.items = homeItems
print("screendismiss")
screenView.dismiss()
weakSelf?.leftableview.reloadData()
}
}
/// 搜索条件点击
@objc func sortButtonClick(button: UIButton) {
UIView.animate(withDuration: 0.5) {
button.imageView?.transform = button.imageView!.transform.rotated(by: CGFloat(M_PI))
}
clicknum = 1
popView.show()
orderView.dismiss()
screenView.dismiss()
}
/// 搜索条件点击
@objc func orderButtonClick(button: UIButton) {
UIView.animate(withDuration: 0.5) {
button.imageView?.transform = button.imageView!.transform.rotated(by: CGFloat(M_PI))
}
clicknum = 2
orderView.show()
popView.dismiss()
screenView.dismiss()
}
@objc func screenButtonClick(button:UIButton){
UIView.animate(withDuration: 0.5) {
button.imageView?.transform = button.imageView!.transform.rotated(by: CGFloat(M_PI))
}
screenView.show()
orderView.dismiss()
popView.dismiss()
}
private lazy var popView: bourseSortTableView = {
let popView = bourseSortTableView()
for i in 0..<cells.count {
popView.cells.append(cells[i])
popView.sorts.append(sorts[i])
}
popView.initRect = CGRect(x: 30, y: 66, width: 110, height: 350)
popView.delegate = self
return popView
}()
private lazy var orderView: bourseSortTableView = {
let orderView = bourseSortTableView()
for i in 0..<ordercells.count {
orderView.cells.append(ordercells[i])
orderView.sorts.append(ordersorts[i])
}
orderView.initRect = CGRect(x: 111, y: 66, width: 110, height: 110)
orderView.delegate = self
return orderView
}()
private lazy var screenView: bourseScreenView = {
let screenView = bourseScreenView()
screenView.initRect = CGRect(x: 55, y: 130, width: 350, height: 350)
screenView.delegate = self
return screenView
}()
private lazy var arrowButton: UIButton = {
let arrowButton = UIButton()
arrowButton.setTitleColor(UIColor.black, for: .normal)
arrowButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
arrowButton.addTarget(self, action: #selector(sortButtonClick), for: .touchUpInside)
arrowButton.setTitle(cells[0], for: .normal)
arrowButton.setImage(UIImage(named: "arrow_index_down_8x4_"), for: .normal)
return arrowButton
}()
private lazy var orderButton: UIButton = {
let orderButton = UIButton()
orderButton.setTitleColor(UIColor.black, for: .normal)
orderButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
orderButton.addTarget(self, action: #selector(orderButtonClick), for: .touchUpInside)
orderButton.setImage(UIImage(named: "arrow_index_down_8x4_"), for: .normal)
orderButton.setTitle(ordercells[0], for: .normal)
return orderButton
}()
private lazy var screenButton: UIButton = {
let screenButton = UIButton()
screenButton.setTitleColor(UIColor.black, for: .normal)
screenButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
screenButton.addTarget(self, action: #selector(screenButtonClick), for: .touchUpInside)
screenButton.setImage(UIImage(named: "arrow_index_down_8x4_"),for:.normal)
screenButton.setTitle("筛选", for: .normal)
return screenButton
}()
func loadData() {
weak var weakSelf = self
NetWorkTool.shareNetworkTool.loadHomeInfo(status: "", cointype: "", order: order, sort: sortnum, type: "", page: boursepagenum) { (homeItems) in
weakSelf!.items = homeItems
if self.type == 0{
weakSelf?.leftableview.reloadData()
}
else if (self.type == 1){
// weakSelf?.rightableview.reloadData()
}
boursepagenum = boursepagenum + 1
}
}
//底部上拉加载
@objc func footerLoad(){
print("上拉加载.")
sleep(2)
//生成并添加数据
loadHomeData()
if self.type == 0{
//重现加载表格数据
self.leftableview!.reloadData()
//结束刷新
self.leftableview!.mj_footer.endRefreshing()
}
else if (self.type == 1){
//重现加载表格数据
self.rightableview!.reloadData()
//结束刷新
self.rightableview!.mj_footer.endRefreshing()
}
}
@objc func loadHomeData() {
// 获取数据
weak var weakSelf = self
NetWorkTool.shareNetworkTool.loadHomeInfo(status: "", cointype: "",order: order, sort: sortnum, type: "", page: boursepagenum) { (homeItems) in
for item in homeItems {
weakSelf!.items.append(item)
}
boursepagenum = boursepagenum + 1
}
}
/// 创建 tableView
private func setupTableView() {
leftableview = UITableView.init(frame:CGRect.init(x: 0, y: 125, width: SCREENW, height: 603),style:UITableViewStyle.plain)
let lnib = UINib(nibName: String(describing: StarsTableViewCell.self), bundle: nil)
leftableview.register(lnib, forCellReuseIdentifier: StarsCellID)
leftableview.rowHeight = 160
leftableview.separatorStyle = .none
leftableview!.delegate = self
leftableview!.dataSource = self
self.view.addSubview(leftableview!)
rightableview = UITableView.init(frame:CGRect.init(x: 0, y: 125, width: SCREENW, height: 603),style:UITableViewStyle.plain)
let rnib = UINib(nibName: String(describing: StarsTableViewCell.self), bundle: nil)
rightableview.register(rnib, forCellReuseIdentifier: StarsCellID)
rightableview.rowHeight = 160
rightableview.separatorStyle = .none
rightableview!.delegate = self
rightableview!.dataSource = self
self.view.addSubview(rightableview!)
leftableview.isHidden = false
rightableview.isHidden = true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if type == 0{
return items.count
}
else if (type == 1){
return items.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//设置单元格数据
if type == 0{
let homeCell = tableView.dequeueReusableCell(withIdentifier: StarsCellID) as! StarsTableViewCell
homeCell.selectionStyle = .none
print("数据1",indexPath.row)
homeCell.homeItem = items[indexPath.row]
return homeCell
}
else if (type == 1){
let homeCell = tableView.dequeueReusableCell(withIdentifier: StarsCellID) as! StarsTableViewCell
homeCell.selectionStyle = .none
print("数据2",indexPath.row)
homeCell.homeItem = items[indexPath.row]
return homeCell
}
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
return cell
}
}
iOS-swift学习笔记-2
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 原创文章,欢迎转载。转载请注明:关东升的博客 Swift 2.0中的print函数有4种重载形式: print(_...
- 原创文章,欢迎转载。转载请注明:关东升的博客 看了之前的学习笔记知道了什么是关键字,现在提示各位在Swift 2...
- 《从零开始学Swift》学习笔记(Day 2)——使用Web网站编写Swift代码原创文章,欢迎转载。转载请注明:...
- 一、封装请求正文到对象中(非常重要) 1、静态参数封装 在struts.xml配置文件中,给动作类注入值。调用的是...