以编程方式添加的按钮目标操作在自定义 tableviewcell 中不起作用
Programmatically added buttons target action not working in custom tableviewcell
我在自定义 tableviewcell 中以编程方式添加了两个按钮,但目标操作不起作用。我该怎么做才能解决这个问题?(swift)
这是我添加按钮的代码:
override func awakeFromNib() {
super.awakeFromNib()
func createCueButton() -> UIButton{
let button = UIButton()
button.titleLabel?.textColor = UIColor.whiteColor()
button.titleLabel?.font = UIFont.boldSystemFontOfSize(16.0)
return button
}
completeButton = createCueButton()
completeButton.setTitle("Complete", forState: UIControlState.Normal)
completeButton.backgroundColor = UIColor.greenColor()
completeButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
addSubview(completeButton)
deleteButton = createCueButton()
deleteButton.setTitle("Delete", forState: .Normal)
deleteButton.backgroundColor = UIColor.redColor()
deleteButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
addSubview(deleteButton)
}
func buttonPressed(sender: UIButton){
var alertView = UIAlertView();
alertView.addButtonWithTitle("OK");
alertView.title = "Alert";
alertView.message = "Button Pressed!!!";
alertView.show();
}
override func layoutSubviews() {
super.layoutSubviews()
completeButton.frame = CGRect(x: 0, y: 0, width: cuesWidth, height: bounds.size.height)
deleteButton.frame = CGRect(x: bounds.size.width - cuesWidth, y: 0, width: cuesWidth, height: bounds.size.height)
}
我更喜欢你问题的答案,请按照以下步骤操作
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 75
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let identifier = "Custom"
var cell: CustomCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCell
if cell == nil
{
tableViewCustom.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableViewCustom.dequeueReusableCellWithIdentifier(identifier) as? CustomCell
}
//First Button for adding in Custom Cell
let btn = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Complete", forState: UIControlState.Normal)
btn.frame = CGRectMake(0, 6, 80, 40)
btn.addTarget(self, action: "completeButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
cell.contentView.addSubview(btn)
//Second Button for adding in CustomCell
let btn2 = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
btn2.backgroundColor = UIColor.greenColor()
btn2.setTitle("Delete", forState: UIControlState.Normal)
btn2.frame = CGRectMake(180, 5, 80, 40)
btn2.addTarget(self, action: "deleteButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
cell.contentView.addSubview(btn2)
return cell
}
func completeButtonTouched(sender:UIButton!)
{
println("Complete Button Target Action Works!!!")
}
func deleteButtonTouched(sender:UIButton!)
{
println("Delete Button Target Action Works!!!")
}
无论何时以编程方式将按钮添加到自定义单元格,请应用以下代码
cell.contentView.addSubview(btn)
我在自定义 tableviewcell 中以编程方式添加了两个按钮,但目标操作不起作用。我该怎么做才能解决这个问题?(swift)
这是我添加按钮的代码:
override func awakeFromNib() {
super.awakeFromNib()
func createCueButton() -> UIButton{
let button = UIButton()
button.titleLabel?.textColor = UIColor.whiteColor()
button.titleLabel?.font = UIFont.boldSystemFontOfSize(16.0)
return button
}
completeButton = createCueButton()
completeButton.setTitle("Complete", forState: UIControlState.Normal)
completeButton.backgroundColor = UIColor.greenColor()
completeButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
addSubview(completeButton)
deleteButton = createCueButton()
deleteButton.setTitle("Delete", forState: .Normal)
deleteButton.backgroundColor = UIColor.redColor()
deleteButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
addSubview(deleteButton)
}
func buttonPressed(sender: UIButton){
var alertView = UIAlertView();
alertView.addButtonWithTitle("OK");
alertView.title = "Alert";
alertView.message = "Button Pressed!!!";
alertView.show();
}
override func layoutSubviews() {
super.layoutSubviews()
completeButton.frame = CGRect(x: 0, y: 0, width: cuesWidth, height: bounds.size.height)
deleteButton.frame = CGRect(x: bounds.size.width - cuesWidth, y: 0, width: cuesWidth, height: bounds.size.height)
}
我更喜欢你问题的答案,请按照以下步骤操作
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 75
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let identifier = "Custom"
var cell: CustomCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCell
if cell == nil
{
tableViewCustom.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableViewCustom.dequeueReusableCellWithIdentifier(identifier) as? CustomCell
}
//First Button for adding in Custom Cell
let btn = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Complete", forState: UIControlState.Normal)
btn.frame = CGRectMake(0, 6, 80, 40)
btn.addTarget(self, action: "completeButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
cell.contentView.addSubview(btn)
//Second Button for adding in CustomCell
let btn2 = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
btn2.backgroundColor = UIColor.greenColor()
btn2.setTitle("Delete", forState: UIControlState.Normal)
btn2.frame = CGRectMake(180, 5, 80, 40)
btn2.addTarget(self, action: "deleteButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
cell.contentView.addSubview(btn2)
return cell
}
func completeButtonTouched(sender:UIButton!)
{
println("Complete Button Target Action Works!!!")
}
func deleteButtonTouched(sender:UIButton!)
{
println("Delete Button Target Action Works!!!")
}
无论何时以编程方式将按钮添加到自定义单元格,请应用以下代码
cell.contentView.addSubview(btn)