创建表格 自定义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)
}
完事了…