Swift表格呼叫model
阿新 • • 發佈:2018-12-12
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: self.view.frame, style: .plain) 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 } }