Siwft 表格简单变色tag值

import UIKit

class oneViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil{
            cell = UITableViewCell(style: .subtitle, reuseIdentifier:"cell")
            
        }
        bun = UIButton(frame: CGRect(x: 300, y: 10, width: 100, height: 30))
        bun?.setTitle("详情", for: .normal)
        bun?.setTitleColor(.red, for: .normal)
        bun?.tag = indexPath.row
        bun?.addTarget(self, action: #selector(heihei(btn:)), for: .touchUpInside)
        
        cell.addSubview(bun!)
        cell.imageView?.image = UIImage(named:"wo_H")
        cell.textLabel?.text = "测试数据"
        cell.backgroundColor = UIColor(red: (CGFloat(Float(arc4random()%255)/255.0)), green: (CGFloat(Float(arc4random()%255)/255.0)), blue: (CGFloat(Float(arc4random()%255)/255.0)), alpha: 1.0)
        
       return cell
    }
    

var table = UITableView()
    var bun:UIButton?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "周考作品"
        table = UITableView(frame: UIScreen.main.bounds, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.rowHeight = 70
        
        self.view .addSubview(table)
        let right = UIBarButtonItem(barButtonSystemItem: .add, target: self, action:#selector(abc))
        self.navigationItem.rightBarButtonItem=right
        
        
        
        
    }
    @objc func abc(){
        print("点我干嘛?没事拜拜!")
    }

    @objc func heihei(btn:UIButton){
        print("第\(btn.tag)个")
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0{
            print("这里是第一个cell哦")
        }else if indexPath.row == 1{
            print("这里是第2个cell哦")
        }else if indexPath.row == 2{
            print("这里是第3个cell哦")
        }else if indexPath.row == 3{
            print("这里是第4个cell哦")
        }else if indexPath.row == 4{
            print("这里是第5个cell哦")
        }else{
            print("这里是第我也不知道哪个cell哦")
        }
    }
    
    
    
    

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值