IOS

TableView的使用-布局重用

2019-01-14  本文已影响0人  瑟闻风倾

1. 问题描述

如图所示,若消费明细和充值明细的界面样式完全一样,只不过请求到的数据不同,此时可用一个控制器(ConsumeTableViewControl)来实现消费明细和充值明细(用户界面使用的是静态单元格,消费明细和充值明细共用的界面使用的是动态单元格)


消费明细.png
充值明细.png
重用布局.png

2. 实现重用布局

(1)选中用户界面的"消费明细"单元格,并按住"control"拖动到明细界面后选择"show"来实现"用户界面"到"消费明细"界面;充值明细的转场操作同上.


界面转场.png

(2)UserTableViewController.swift

//
//  UserTableViewController.swift
//  JackUChat
//
//  Created by 徐云 on 2019/1/12.
//  Copyright © 2019 Liy. All rights reserved.
//

import UIKit

class UserTableViewController: UITableViewController {
    
    @IBOutlet weak var accountViewCell: UITableViewCell!
    @IBOutlet weak var consumeViewCell: UITableViewCell!
    @IBOutlet weak var rechargeViewCell: UITableViewCell!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // 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 0
//    }

    /*
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        
        // Configure the cell...
        
        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
    }
    */

    
    // 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.
        let defaults = UserDefaults.standard
        if(consumeViewCell.isSelected){
            defaults.set(1, forKey: "selectRow")
        }else if(rechargeViewCell.isSelected){
            defaults.set(2, forKey: "selectRow")
        }
    }

}

(3) ConsumeTableViewController.swift

//
//  ConsumeTableViewController.swift
//  JackUChat
//
//  Created by 徐云 on 2019/1/14.
//  Copyright © 2019 Liy. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class ConsumeTableViewController: UITableViewController {
    
    var orders:[Order] = []
    
    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
        
        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 orders.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: ConsumeCell.self)
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ConsumeCell
        let order = orders[indexPath.row]
        cell.consumeLabel.text = order.consume
        cell.balanceLabel.text = order.balance
        cell.detailLabel.text = order.detail
        cell.timeLabel.text = order.date
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150.0
    }
 

    /*
    // 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 t = ""//标志消费明细和充值明细
        let defaults = UserDefaults.standard
        let selectRow = defaults.integer(forKey: "selectRow")
        if (selectRow == 1) {
            t = "xf"
        }else if (selectRow == 2) {
            t = "cz"
        }
        let params:Parameters = ["page":"1","t":t]
        AlamofireHelper.shareInstance.requestData(.post, url: "account/detail", 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 logList = jsonDictory["data"]["logList"].array
                for log in logList!{
                    let order = Order(consume: log["money"].string ?? "", balance: log["account"].string ?? "", detail: log["detail"].string ?? "", date: log["time"].string ?? "")
                    self.orders.append(order)
                }
                
                dump(self.orders)//打印
                //异步获取数据,需在主线程中更新
                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.
    }
    */

}

(4) Order.swift

//
//  Order.swift
//  JackUChat
//
//  Created by 徐云 on 2019/1/14.
//  Copyright © 2019 Liy. All rights reserved.
//

struct Order:Codable {
    var consume:String
    var balance:String
    var detail:String
    var date:String
    
    
}

上一篇下一篇

猜你喜欢

热点阅读