迅速-不执行自定义TableView单击UIButton [英] Swift - Custom TableView Not Executing Click on UIButton

查看:51
本文介绍了迅速-不执行自定义TableView单击UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swift来完成我认为简单的任务。我创建了一个自定义TableView单元格,并向其中添加了一个按钮。我希望能够看到单击按钮时发生的某些动作。不幸的是,当我单击按钮时,没有任何反应。有人可以阐明这一点吗?我将代码放在下面:

I am trying to do what I believed was a simple task using Swift. I have created a Custom TableView Cell and added a button to it. I want to be able to just see when you click on the button some action take place. Unfortunately, when I click on the button there is nothing happening. Can someone shed some light on this? I have placed the code below:

自定义TableView单元格:

Custom TableView Cell:

import UIKit

class TestTableViewCell: UITableViewCell {

@IBOutlet weak var testBtn: UIButton!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
func buttonTappedOnCell()
{
    println("buttonTapped!")
}

}

下面是在视图控制器中正在进行调用的代码:

Below is the code in the View Controller which the call is being made:

  func tableView(tableView: UITableView,
    cellForRowAtIndexPath
    indexPath: NSIndexPath) -> UITableViewCell {
        tableView.registerNib(UINib(nibName:"TestTableViewCell", bundle:nil), forCellReuseIdentifier:"TestButtonCell")
 let cell =
        tableView.dequeueReusableCellWithIdentifier("TestButtonCell", forIndexPath: indexPath) as TestTableViewCell

           cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchDown)
        return cell
}

我也尝试更改调用以包括':'-> cell.testBtn.addTarget(cell,action: buttonTappedOnCell: ,forControlEvents:UIControlEvents.TouchDown),但这无效。我尝试过的另一件事是将调用函数移入视图控制器,并使目标操作指向self。不幸的是,这也不起作用。一个简短的例子可以很好地说明这一工作。归根结底,我们希望能够拥有一个包含6个单选按钮的自定义表格视图单元,允许用户仅选择一个。但是,此时婴儿要先走。谢谢您的帮助。

I also tried changing the call to include the ':' -> cell.testBtn.addTarget(cell, action: "buttonTappedOnCell:", forControlEvents: UIControlEvents.TouchDown) but this did not work. Another thing I tried is moving the call function into the view controller and having the target action point to self. This unfortunately did not work either. A quick example of this working would be great. At the end of day would like to be able to have a custom table view cell that contains 6 radio buttons allowing the user to select just one. But baby steps first at this point. Thanks for any help.

推荐答案

我认为问题是由您在代码中指定的事件引起的。

I think the issue is caused by the event you specified in the code.

 cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchDown)

ToouchDown 更改为 TouchUpInside

cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchUpInside)

为什么还要通过编程方式设置它?根据您的代码,我认为您在情节提要板上有按钮。那么,为什么要以编程方式添加IBAction? (因为该操作也被同一类调用)。

Also why are you setting it programmaticaly ? From your code I think you have the button on your storyboard. So why should you add the IBAction programmaticaly ? (Since the action is also invoked on the same class).

将您的 buttonTappedOnCell 声明为IBAction并将其连接直接单击您在情节提要/ xib上的按钮。

Declare your buttonTappedOnCell as an IBAction and connect it directly to your button on storyboard/xib.

这篇关于迅速-不执行自定义TableView单击UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆