Swift底部控制器TableBar及导航栏

2018-10-19  本文已影响0人  GraySuit

在创建完控制器后在APPDelegate.swift中


屏幕快照 2018-10-19 16.08.14.png
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    let vc1 = OneVC()
    let nav1 = UINavigationController(rootViewController: vc1)
    nav1.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let vc2 = TwoVC()
    let nav2 = UINavigationController(rootViewController: vc2)
    nav2.tabBarItem = UITabBarItem(title: "返现", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let vc3 = ThreeVC()
    let nav3 = UINavigationController(rootViewController: vc3)
    nav3.tabBarItem = UITabBarItem(title: "购物", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let vc4 = FourVC()
    let nav4 = UINavigationController(rootViewController: vc4)
    nav4.tabBarItem = UITabBarItem(title: "我的", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
    
    let tbv = UITabBarController()
    tbv.viewControllers = [nav1,nav2,nav3,nav4]
    tbv.selectedViewController = nav2
    self.window?.rootViewController = tbv
    
    return true
}

在TwoVC.swift中
import UIKit

class CellData: NSObject {

static func getCellData()->[musicModel]{
    
    let arr1 = ["男士衬衫","男士衬衫","男士衬衫","男士衬衫","男士衬衫","男士衬衫","男士衬衫","男士衬衫"]
    let arr2 = ["09:17:00","09:17:00","09:17:00","09:17:00","09:17:00","09:17:00","09:17:00","09:17:00"]
    let arr3 = ["96件","96件","96件","96件","96件","96件","96件","96件"]
    let imgName = ["1","1","1","1","1","1","1","1"]
    var mArr:[musicModel] = []
    
    for i in 0..<arr1.count {
        
        let md = musicModel(title: arr1[i], pic: imgName[i], number: arr3[i], time: arr2[i])
        mArr.append(md)
    }
    return mArr
}

}

import UIKit

class TwoVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var mArr:[musicModel] = CellData.getCellData()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.white
    
    self.navigationItem.title = "商品"
    self.navigationController?.navigationBar.barTintColor = UIColor.purple
    
    let leftBtn = UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(leftButton))
    self.navigationItem.leftBarButtonItem = leftBtn
    
    let tv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    self.view.addSubview(tv)
    
    tv.delegate = self
    tv.dataSource = self
    
    tv.register(UINib(nibName: "MusicCell", bundle: Bundle.main), forCellReuseIdentifier: "Cell1")
    
    let viewHead = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 150))
    tv.tableHeaderView = viewHead
    
    let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 150))
    viewHead.addSubview(imgView)
    imgView.image = UIImage(named: "1")
    
    let viewBack = UIView(frame: CGRect(x: 0, y: viewHead.frame.size.height-60, width: UIScreen.main.bounds.width, height: 60))
    viewHead.addSubview(viewBack)
    viewBack.backgroundColor = UIColor.lightGray
    
    
    let imgHead = UIImageView(frame: CGRect(x: 20, y: (viewBack.frame.size.height-40)/2, width: 40, height: 40))
    viewBack.addSubview(imgHead)
    imgHead.image = UIImage(named: "1")
    
    let title = UILabel(frame: CGRect(x: 20+imgHead.frame.size.width+5, y: (viewBack.frame.size.height-40)/2, width: 150, height: 40))
    viewBack.addSubview(title)
    title.text = "速记播放"
    
}

@objc func leftButton() -> Void {
    print("点击左键")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return mArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell:MusicCell = tableView.dequeueReusableCell(withIdentifier: "Cell1") as! MusicCell
    
    let md:musicModel = mArr[indexPath.row]
    
    cell.setCellWithData(model: md)
    
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    
    return 100
}

}

在MusicCell中
import UIKit

class MusicCell: UITableViewCell {

@IBOutlet weak var titleLabel: UILabel!

@IBOutlet weak var timeLabel: UILabel!

@IBOutlet weak var numberLabel: UILabel!

@IBOutlet weak var picImgView: UIImageView!

func setCellWithData(model:musicModel) -> Void {
    
    self.titleLabel.text = model.title
    self.timeLabel.text = model.time
    self.numberLabel.text = model.number
    self.picImgView.image = UIImage(named: model.pic)
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

屏幕快照 2018-10-25 11.12.25.png

在musicModel中
import UIKit

class musicModel: NSObject {

var title:String
var pic:String
var number:String
var time:String
init(title:String,pic:String,number:String,time:String) {
    self.title = title
    self.pic = pic
    self.number = number
    self.time = time
}

}

上一篇下一篇

猜你喜欢

热点阅读