Swift传值

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

完事了…
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值