简体   繁体   English

来自UITableViewCell内的UIButton的IBAction

[英]IBAction from a UIButton inside a UITableViewCell

I created a UITableViewCell in Interface Builder which I have added a UIImageView and a UIButton to. 我在接口生成器中创建了一个UITableViewCell,在其中添加了UIImageView和UIButton。 I have given the UIButton an outlet, and hooked it up to a IBAction with the touch up inside event set. 我给了UIButton一个插座,并将其与touchup内部事件集一起连接到IBAction上。 I assumed it would call this method, as everything looked like it is hooked up: 我以为它会调用此方法,因为一切看起来都与它挂钩:

- (IBAction)pressedCheckbox:(id)sender {
    [sender setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    NSLog(@"clicked check");

}

Checking the connections tab, I have everything set correctly, the outlet and the action. 检查连接选项卡,我一切设置正确,插座和操作正确。 And I get no errors, but when previewing the app, clicking on that button inside the UITableViewCell does nothing, and I don't see anything in the logs. 而且我没有收到任何错误,但是在预览应用程序时,单击UITableViewCell内的该按钮不会执行任何操作,并且日志中也看不到任何内容。

Ideas on why it won't let me click my button which is in a UITableViewCell? 关于为什么它不能让我单击UITableViewCell中的按钮的想法?

EDIT: 编辑:

Code for cellForRowAtIndexPath: cellForRowAtIndexPath的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                cell = topCell;
                break;
            case 1: 
                cell = cell1;
                break;
        }//end switch

    }//end if

    return cell;
}

I also figured out what the problem was. 我还弄清楚了问题所在。 I was needing to set different heights for cells and was using: 我需要为单元格设置不同的高度,并且正在使用:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

     if (indexPath.section == 0) {

          switch (indexPath.row) {

               case 0:

                    return 26;

                    break;

          }//end switch

     }//end if

     return tableView.rowHeight; <-- I just added this and it fixed the issue

}

Previously I was only returning a value for cell 0, so it was causing issues with all my other cells 以前我只是返回单元格0的值,所以这导致我所有其他单元格出现问题

Are you doing this in IB? 您是在IB执行此操作吗? It may be worth trying - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents (in the loadView method of the owning view controller) to see if the problem is the IB link. 可能值得尝试- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents (在拥有的视图控制器的loadView方法中)的动作,看问题是否出在IB链接上。

You mean that this is a subclass of UITableViewCell right? 您的意思是这是UITableViewCell的子类,对吗? Are you sure that you are using this instead of the normal UITableViewCell? 您确定使用的是它而不是普通的UITableViewCell吗?

Go to your cellForRowAtIndexPath method implementation and look for the line where you instantiate the cell. 转到cellForRowAtIndexPath方法实现,然后在实例化单元格的那一行中查找。 Make sure it's instantiating it to whatever your subclass is called, and not UITableViewCell. 确保将其实例化到您的子类调用的任何实例,而不是 UITableViewCell的实例。 If you're confused, post the code for your codeForRowAtIndexPath method so we can see it. 如果您感到困惑,请为您的codeForRowAtIndexPath方法发布代码,以便我们查看。

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

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