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哦")
}
}
}
Siwft 表格简单变色tag值
最新推荐文章于 2023-05-15 23:08:07 发布