【发布时间】:2018-01-02 16:51:07
【问题描述】:
如何在表格单元格内使(提供)按钮执行操作,如图所示: this picture of table view include suctions , cells then inside it the button ( make offer ) button
我正在使用此代码,但是当我单击它时会出错
错误: error
这是代码
// tabels functions
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
// make section tachuble
view.backgroundColor = UIColor.white
let SW = UIScreen.main.bounds.width
let Ordlbl = UILabel()
Ordlbl.text = "Header"
Ordlbl.frame = CGRect (x: 16, y: 0, width: SW - 16, height: 40)
Ordlbl.font = UIFont(name:"Courier-bold", size: 20.0)
view.addSubview(Ordlbl)
let fromlbl = UILabel()
fromlbl.text = "A12345678"
fromlbl.frame = CGRect (x: 16, y: 20, width: SW - 16, height: 40)
fromlbl.textAlignment = NSTextAlignment.left
fromlbl.font = UIFont(name:"Courier", size: 20.0)
view.addSubview(fromlbl)
let deslbl = UILabel()
deslbl.text = "- 20 KM ->"
deslbl.textAlignment = NSTextAlignment.center
deslbl.font = UIFont(name:"Courier", size: 20.0)
deslbl.frame = CGRect (x: 0, y: 20,width: SW - 16, height: 40)
view.addSubview(deslbl)
let tolbl = UILabel()
tolbl.text = "B12345678"
tolbl.textAlignment = NSTextAlignment.right
tolbl.font = UIFont(name:"Courier", size: 20.0)
tolbl.frame = CGRect (x: 0, y: 20, width: SW - 16, height: 40)
view.addSubview(tolbl)
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.init(red: 234/255, green: 238/255, blue: 237/255, alpha: 1.00)
let SW = UIScreen.main.bounds.width
let deslbl = UILabel()
deslbl.text = "Order Description :"
deslbl.textAlignment = NSTextAlignment.left
deslbl.font = UIFont(name:"Courier-Bold", size: 17.5)
deslbl.frame = CGRect (x: 16, y: 16, width: SW - 68, height: 40)
cell.addSubview(deslbl)
let deslbls = UILabel()
deslbls.text = "fgwerhioghefrege gergu eriguerg erg uerg peor ger gey gerop gpoergrg er grgporg eropg yergy erpog regy reyg poreg oper goerygpre gyergoer goyer goer goy ergy erg er gyeogp yerop gyoery georgy eroy gpoer gyoer goepr gopre goper gpo ergo ery goper gyroepgy repo gyreopyg repogy peroyg peorg yerpog eropg yeropg yeropg yerop"
deslbls.textAlignment = NSTextAlignment.left
deslbls.lineBreakMode = .byWordWrapping
deslbls.numberOfLines = 6
deslbls.font = UIFont(name:"Courier", size: 15.0)
deslbls.frame = CGRect (x: 16, y: 42, width: SW - 68, height: 100)
cell.addSubview(deslbls)
let Timelbl = UILabel()
Timelbl.text = "60"
Timelbl.textAlignment = NSTextAlignment.right
Timelbl.font = UIFont(name:"Courier-Bold", size: 35.0)
Timelbl.frame = CGRect (x: SW - 70, y: 16, width: 50, height: 50)
cell.addSubview(Timelbl)
let Timelbls = UILabel()
Timelbls.text = "min"
Timelbls.textAlignment = NSTextAlignment.right
Timelbls.font = UIFont(name:"Courier-Bold", size: 10)
Timelbls.frame = CGRect (x: SW - 82, y: 35, width: 50, height: 50)
cell.addSubview(Timelbls)
let textin = UITextField()
textin.text = "20"
textin.backgroundColor = UIColor.white
textin.textAlignment = NSTextAlignment.center
textin.font = UIFont(name:"Courier-Bold", size: 20)
textin.frame = CGRect (x: 16, y: 150, width: SW - 124, height: 35)
cell.addSubview(textin)
let Btin = UIButton()
Btin.backgroundColor = UIColor.init(red: 127/255, green: 192/255, blue: 0/255, alpha: 1.00)
Btin.frame = CGRect (x: SW - 100, y: 150, width: 90, height: 35)
Btin.tag = indexPath.row
Btin.addTarget(self, action: Selector("printC"), for: UIControlEvents.touchUpInside)
cell.addSubview(Btin)
let Btinlbl = UILabel()
Btinlbl.text = "Make Offer"
Btinlbl.lineBreakMode = .byWordWrapping
Btinlbl.numberOfLines = 2
Btinlbl.textAlignment = NSTextAlignment.center
Btinlbl.font = UIFont(name:"Courier-Bold", size: 15)
Btinlbl.frame = CGRect (x: SW - 95, y: 150, width: 80, height: 35)
cell.addSubview(Btinlbl)
return cell
}
func printC(){
print("ok")
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
// end table
这段代码是我的问题:
let Btin = UIButton()
Btin.backgroundColor = UIColor.init(red: 127/255, green: 192/255, blue: 0/255, alpha: 1.00)
Btin.frame = CGRect (x: SW - 100, y: 150, width: 90, height: 35)
Btin.tag = indexPath.row
Btin.addTarget(self, action: Selector("printC"), for: UIControlEvents.touchUpInside)
cell.addSubview(Btin)
它的功能是这样的:
func printC(){
print("ok")
}
【问题讨论】:
-
您的选择器不正确。尝试正确的。
-
为什么不选择storyBoard作为设计目的?
-
正确的解决方案是创建一个自定义表格视图单元格并使用委托。这应该会有所帮助:stackoverflow.com/questions/43627355/…
-
好的,抱歉。下次会仔细看看@Moritz
标签: ios iphone swift uitableview