首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

TableViewCell中用于UIButton的addTarget不起作用

在TableViewCell中使用UIButton的addTarget不起作用的问题,可能是由于以下几个原因导致的:

  1. 检查是否正确设置了UIButton的target和action。在TableViewCell中,通常在cell的初始化方法或者cell的配置方法中设置UIButton的target和action。确保target是正确的对象,action是正确的方法。
  2. 检查是否正确设置了UIButton的事件类型。UIButton有多种事件类型,如TouchUpInside、TouchDown等。确保选择了正确的事件类型来触发按钮的动作。
  3. 检查是否正确设置了UIButton的frame。如果UIButton的frame设置不正确,可能导致按钮无法响应点击事件。确保按钮的frame在可见区域内,并且不被其他视图遮挡。
  4. 检查是否正确设置了UITableViewCell的userInteractionEnabled属性。如果UITableViewCell的userInteractionEnabled属性被设置为NO,那么其中的所有子视图,包括UIButton,都无法响应用户的交互事件。确保UITableViewCell的userInteractionEnabled属性被设置为YES。

如果以上方法都没有解决问题,可以尝试以下几个调试步骤:

  1. 在UIButton的addTarget方法之前,添加一行代码打印UIButton的相关信息,如按钮的frame、target、action等。确保这些信息都是正确的。
  2. 在UIButton的addTarget方法之后,添加一行代码打印UIButton的所有绑定的事件。确保UIButton的事件绑定成功。
  3. 在UITableViewCell的didSelectRowAtIndexPath方法中,添加一行代码打印被点击的UITableViewCell的indexPath。确保点击的是正确的UITableViewCell。

如果以上方法都无法解决问题,可能需要进一步检查代码逻辑或者提供更多的代码信息来进行问题排查。

推荐的腾讯云相关产品:腾讯云移动推送(https://cloud.tencent.com/product/umeng_push)、腾讯云移动直播(https://cloud.tencent.com/product/mlvb)、腾讯云云服务器(https://cloud.tencent.com/product/cvm)、腾讯云云数据库 MySQL 版(https://cloud.tencent.com/product/cdb_mysql)、腾讯云对象存储(https://cloud.tencent.com/product/cos)等。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 史上最全的iOS之访问自定义cell的textField.text的N种方法

    问题背景:自定义cell中有一个UITextField类型的子控件。我们经常要在tableView中拿到某个cell内textField的文本内容进行一些操作。比如某些app的注册界面就是以tableView的形式存在的,注册时往往需要注册姓名、昵称、邮箱、地址、联系方式等信息。然后点击注册或者提交,这些信息就会被提交到远程服务器。有人说,注册页面就那么固定的几行cell,没必要搞得那么复杂,完全可以用静态cell实现。但还有一些情况,当前页面的tableView的cell的行数是不确定的(比如当前页面显示多好行cell由上一个页面决定或者由用户决定),这种情况下不太适合使用静态cell。也不能够通过分支语句的方式一一枚举出各个case。所以需要一中通用的动态的方法。那么我们怎么在tableView中准确的拿到每一行cell中textField的text呢?以下我将要分四个方法分别介绍并逐一介绍他们的优缺点,大家可以在开发中根据实际情况有选择的采用不同的方法。 如下图,就是我之前开发的一个app中用xib描述的一个cell,当用户点击“注册”或者“提交”button时候,我需要在控制器中拿到诸如“法人姓名”这一类的信息:

    04

    IOS 弹出框

    2、弹出框: import UIKit class ViewController:UIViewController { var label:UILabel! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.brown label = UILabel(frame:CGRect(x:40, y:100,width:240, height:44)) label.text = ”” self.view.addSubview(label) let button = UIButton(frame:CGRect(x:40, y:180,width:240, height:44)) button.setTitle(“打开新的视图控制器”, for:UIControlState()) button.backgroundColor = UIColor.black button.addTarget(self, action:#selector(ViewController.openViewController),fo:.touchUpInside) self.view.addSubview(button) } func openViewController() { let newViewController = NewViewController() newViewController.labelTxt = “传递的参数!” newViewController.viewController = self self.present(newViewController, animated:true,completion:nil) } }

    05

    IOS移动开发从入门到精通 视图UIView、层CALayer(2)

    或者修改 rootViewController参数 2、弹出框: import UIKit class ViewController:UIViewController { var label:UILabel! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.brown label = UILabel(frame:CGRect(x:40, y:100,width:240, height:44)) label.text = ”” self.view.addSubview(label) let button = UIButton(frame:CGRect(x:40, y:180,width:240, height:44)) button.setTitle(“打开新的视图控制器”, for:UIControlState()) button.backgroundColor = UIColor.black button.addTarget(self, action:#selector(ViewController.openViewController),fo:.touchUpInside) self.view.addSubview(button) } func openViewController() { let newViewController = NewViewController() newViewController.labelTxt = “传递的参数!” newViewController.viewController = self self.present(newViewController, animated:true,completion:nil) } }

    01
    领券