import UIKit
import SVProgressHUD
class bourseViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,StarsSortTableViewDelegate,StarsScreenViewDelegate {
var cells = ["默認排序", "按價格排序", "按代數(shù)排序", "按生命排序", "按品質(zhì)排序", "按等級排序", "按文明排序", "按探索排序"]
/// 排序方式
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()
//上刷新相關(guān)設(shè)置
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) //添加值改變監(jiān)聽
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) {
/// 根據(jù)搜索條件進行搜索//排序字段(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)
//加載數(shù)據(jù)
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,默認不選傳空
//加載數(shù)據(jù)
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)
//生成并添加數(shù)據(jù)
loadHomeData()
if self.type == 0{
//重現(xiàn)加載表格數(shù)據(jù)
self.leftableview!.reloadData()
//結(jié)束刷新
self.leftableview!.mj_footer.endRefreshing()
}
else if (self.type == 1){
//重現(xiàn)加載表格數(shù)據(jù)
self.rightableview!.reloadData()
//結(jié)束刷新
self.rightableview!.mj_footer.endRefreshing()
}
}
@objc func loadHomeData() {
// 獲取數(shù)據(jù)
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
}
}
/// 創(chuàng)建 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 {
//設(shè)置單元格數(shù)據(jù)
if type == 0{
let homeCell = tableView.dequeueReusableCell(withIdentifier: StarsCellID) as! StarsTableViewCell
homeCell.selectionStyle = .none
print("數(shù)據(jù)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("數(shù)據(jù)2",indexPath.row)
homeCell.homeItem = items[indexPath.row]
return homeCell
}
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
return cell
}
}
iOS-swift學(xué)習(xí)筆記-2
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蝙昙,“玉大人闪萄,你說我怎么就攤上這事∑娴撸” “怎么了败去?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長烈拒。 經(jīng)常有香客問我圆裕,道長广鳍,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任吓妆,我火速辦了婚禮赊时,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘行拢。我一直安慰自己祖秒,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布舟奠。 她就那樣靜靜地躺著竭缝,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沼瘫。 梳的紋絲不亂的頭發(fā)上抬纸,一...
- 文/蒼蘭香墨 我猛地睜開眼飞几,長吁一口氣:“原來是場噩夢啊……” “哼砚哆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起屑墨,我...
- 正文 年R本政府宣布,位于F島的核電站谨垃,受9級特大地震影響启搂,放射性物質(zhì)發(fā)生泄漏硼控。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一胳赌、第九天 我趴在偏房一處隱蔽的房頂上張望淀歇。 院中可真熱鬧,春花似錦匈织、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至碰逸,卻和暖如春乡小,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背饵史。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 原創(chuàng)文章,歡迎轉(zhuǎn)載讲竿。轉(zhuǎn)載請注明:關(guān)東升的博客 Swift 2.0中的print函數(shù)有4種重載形式: print(_...
- 原創(chuàng)文章泥兰,歡迎轉(zhuǎn)載。轉(zhuǎn)載請注明:關(guān)東升的博客 看了之前的學(xué)習(xí)筆記知道了什么是關(guān)鍵字题禀,現(xiàn)在提示各位在Swift 2...
- 《從零開始學(xué)Swift》學(xué)習(xí)筆記(Day 2)——使用Web網(wǎng)站編寫Swift代碼原創(chuàng)文章鞋诗,歡迎轉(zhuǎn)載。轉(zhuǎn)載請注明:...
- 一削彬、封裝請求正文到對象中(非常重要) 1、靜態(tài)參數(shù)封裝 在struts.xml配置文件中秀仲,給動作類注入值吃警。調(diào)用的是...