Swift傳值
阿新 • • 發佈:2018-12-15
建立表格 自定義xib設定位置 當我們點選表格的時候進行跳轉傳值
1.導航控制器
2.tabbar
3.我們來建立一個繼承NSObject名為CellData的檔案…
(1).在裡面寫內容
(2).圖片
static func getCellData()->[Model] { var dataSource:Array = ["我","是","世","界","最","可","愛","的","帥","哥"] var immm:Array=["1","2","3","4","5","6","7","8","9","10"] var mod:[Model]=[] for i in 0..<dataSource.count{ let md = Model.init(img:immm[i] , text: dataSource[i]) mod.append(md) } return mod }
建立Model檔案繼承NSObject 用來呼叫CellData裡面資料
(1).文字
(2).圖片
var img:String?
var text:String?
init(img:String,text:String) {
self.img=img
self.text=text
}
3.建立TableViewCell 可以用xib
import UIKit class TableViewCell: UITableViewCell { var img = UIImageView() var lab = UILabel() var btn = UIButton() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) self.selectionStyle = .none img=UIImageView.init(frame: CGRect.init(x: 10, y: 10, width: 50, height: 50)) img.layer.masksToBounds=true img.layer.cornerRadius=25 lab=UILabel.init(frame: CGRect.init(x: 70, y: 15, width: 200, height: 30)) btn=UIButton.init(frame: CGRect.init(x: 300, y: 10, width: 70, height: 40)) btn.backgroundColor=UIColor.white btn.setTitleColor(UIColor.black, for: UIControl.State.normal) self.addSubview(img) self.addSubview(lab) self.addSubview(btn) } func setnlll(model:Model) -> Void { self.img.image=UIImage.init(named: model.img!) self.lab.text=model.text } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } }
完事之後我們肯定要寫表格的
import UIKit class OneViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { var table = UITableView() var aaa:[Model] = CellData.getCellData() var nr:[Model] = [] func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 }
呼叫根據CEll位置大小呼叫model中的內容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
tableView.rowHeight=70
let md:Model = aaa[indexPath.row]
cell.setnlll(model: md)
cell.btn.setTitle("詳情", for: UIControl.State.normal)
return cell
}
簽寫協議…如果忘得話是出不來表格的
override func viewDidLoad() {
super.viewDidLoad()
table=UITableView.init(frame: self.view.frame, style: UITableView.Style.plain)
table.delegate=self
table.dataSource=self
view.addSubview(table)
table.register(TableViewCell.self, forCellReuseIdentifier: "cell")
}
我們寫表格跳轉的時候傳值,所以建立一個新的檢視,然後再新的視圖裡面寫個label 用來獲取傳值的內容…
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
nr=aaa
ChuanViewController.suiyi=nr[indexPath.row].text
self.navigationController?.pushViewController(ChuanViewController(), animated: true)
}
var lab = UILabel()
static var suiyi:String?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor=UIColor.lightGray
lab = UILabel.init(frame: CGRect.init(x: 50, y: 200, width: 250, height: 250))
lab.backgroundColor=UIColor.white
lab.text=ChuanViewController.suiyi
lab.textColor=UIColor.black
view.addSubview(lab)
}
完事了…