简体   繁体   English

UITableViewCell IBAction在iOS 7中不起作用

[英]UITableViewCell IBAction not working in iOS 7

What I'm facing is I've developed code in Swift. 我面临的是我已经在Swift中开发了代码。 It has a some custom cells and UIButtons in them. 它具有一些自定义单元格和UIButtons。 The buttons are IBOutlet Connections with properties in their respective Cells and actions against them are bound in the same classes. 这些按钮是IBOutlet连接,在其各自的单元中具有属性,针对它们的操作绑定在同一类中。 Now the Question is that it works perfect in iOS 8. But I don't know what's wrong with iOS 7 whenever I touch the button, the complete cell is tapped and Tableview's didSelectRowAtIndexPath: is called. 现在的问题是它可以在iOS 8上完美运行。但是,我不知道每当我触摸按钮,轻敲整个单元格并调用Tableview的didSelectRowAtIndexPath:时,iOS 7会有什么问题。 What I've done so far is 到目前为止,我所做的是

  • Check .userInteractionEabled for parent views and cell and changing it's values 检查.userInteractionEabled的父视图和单元格并更改其值
  • Remove buttons and re add them as found on different forums but no luck 删除按钮,然后将其重新添加为在其他论坛上发现的按钮,但没有
  • Checked Tableview's delegate methods numberOfSections , numberOfRowsInsection: and heightForRowAtIndexPath: and changed their return types to NSInteger and CGFloat as found on stackoverflow Against a Question. 检查Tableview的委托方法numberOfSectionsnumberOfRowsInsection:heightForRowAtIndexPath:并将它们的返回类型更改为NSInteger和CGFloat,如对问题的stackoverflow所示。

Has anyone else faced this issue and found solution so far? 到目前为止,还有其他人面临这个问题并找到解决方案吗?

Edit 编辑

  • Checked by reseting target action of UIButton after setting values to cell. 将值设置为单元格后,通过重置UIButton的目标操作进行检查。 No Luck 没运气

I have tested your problem. 我已经测试了您的问题。 Its running fine for me. 它对我来说很好。 Please see my code below. 请在下面查看我的代码。

In TestTableViewController.swift 在TestTableViewController.swift中

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as CustomCell

       // Configure the cell...

        cell.testButton.addTarget(cell, action: "testButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)

       return cell

    }

       override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        NSLog("didSelectRowAtIndexPath called")

    }

In CustomCell.swift 在CustomCell.swift中

class CustomCell: UITableViewCell {

    @IBOutlet var testButton: 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 testButtonClicked() {

        NSLog("Button Clicked")

    }

}

When I clicked on the button it logs "Button Clicked" and when clicked on cell it logs "didSelectRowAtIndexPath called". 当我单击该按钮时,它记录为“已单击按钮”,当单击该单元格时,它将记录为“ didSelectRowAtIndexPath被调用”。 You might be doing something wrong in reset() function or second last function. 您可能在reset()函数或倒数第二个函数中做错了。

I have done the code below in my app and it is working fine. 我已经在我的应用程序中完成了以下代码,并且工作正常。

Do not try to code on your custom tableview cell. 不要尝试在自定义tableview单元上进行编码。

You can get values of that cell in your viewcontroller. 您可以在视图控制器中获取该单元格的值。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        static NSString *simpleprice = @"Price";

        price = (PriceCell *)[tableView dequeueReusableCellWithIdentifier:simpleprice];

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PriceCell" owner:self options:nil];

        price = [nib objectAtIndex:0];
        [price.btnPrice addTarget:self action:@selector(buyPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return price;

 }

Hope this helps you. 希望这对您有所帮助。 This works for ios7 and ios8 also. 这也适用于ios7和ios8。

Thanks... 谢谢...

i have faced such issue after long time digging it, i found root cause and it was in Cell XIB 经过长时间的挖掘,我遇到了这样的问题,我找到了根本原因,并且它在Cell XIB中

what i did as Before: 我以前做过的事情:

i) First i have added CustomCell .h and .m . 我)首先我添加了CustomCell .h.m
ii) After then i have added XIB by new file option, you have noticed, there is option like "View" that's i have selected and add in to the bundle. ii)之后,我通过新文件添加了XIB选项,您已经注意到,我已经选择了诸如“查看”之类的选项并将其添加到捆绑包中。 Bind it with my CustomCell.h . 将其与我的CustomCell.h绑定。

Solution : 解决方案:

i) Add XIB by new file option, Choose View in user interface. i)按新文件添加XIB选项,在用户界面中选择查看 ii) In XIB you can see view at left panel. ii)在XIB中,您可以在左侧面板中查看视图。 delete that view. 删除该视图。 iii) Drag and drop there UITableViewCell from User Interface panel. iii)从“用户界面”面板拖放UITableViewCell。 After complete this steps XIB would be seen similar something like below image. 完成此步骤后,将看到XIB类似于下图。

在此处输入图片说明

将您的cell.button Action target to self更改Action target to self ,然后将您的按钮操作方法复制到UITableView可用的viewController类(.mm)文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM