【问题标题】:IBAction from a UIButton inside a UITableViewCell来自 UITableViewCell 内的 UIButton 的 IBAction
【发布时间】:2010-08-26 22:38:55
【问题描述】:

我在 Interface Builder 中创建了一个 UITableViewCell,我添加了一个 UIImageView 和一个 UIButton。我为 UIButton 提供了一个插座,并将其连接到 IBAction 并设置了 touch up inside 事件集。我以为它会调用这个方法,因为一切看起来都像是连接起来的:

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

}

检查连接选项卡,我已正确设置所有内容,包括插座和操作。而且我没有收到任何错误,但是在预览应用程序时,单击 UITableViewCell 内的该按钮没有任何反应,并且我在日志中看不到任何内容。

关于为什么它不允许我点击 UITableViewCell 中的按钮的想法?

编辑:

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;
}

【问题讨论】:

    标签: iphone uitableview ibaction


    【解决方案1】:

    我也发现了问题所在。我需要为单元格设置不同的高度并使用:

    - (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
    
    }
    

    以前我只返回单元格 0 的值,所以它会导致我所有其他单元格出现问题

    【讨论】:

    • 不要返回 "44" - 而是返回 tableView.rowHeight。它可能是 44,但你永远不知道它是否会在未来的某些 iOS 中改变。 “26”也是一样——如果需要的话,得到一些 frame.size.height。
    • 感谢您的提示,我会更改的。
    【解决方案2】:

    你在 IB 做这个吗?可能值得尝试- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents(在所属视图控制器的 loadView 方法中)看看问题是否出在 IB 链接上。

    【讨论】:

    • 否,使用:[cell1Check addTarget:self action:@selector(pressedCheckbox:) forControlEvents:UIControlEventTouchUpInside];也没有用。
    【解决方案3】:

    你的意思是这是 UITableViewCell 的子类,对吧?你确定你使用的是这个而不是普通的 UITableViewCell 吗?

    转到您的cellForRowAtIndexPath 方法实现并查找您实例化单元格的行。确保它将它实例化为您调用的任何子类,并且 不是 UITableViewCell。如果您感到困惑,请发布您的 codeForRowAtIndexPath 方法的代码,以便我们查看。

    【讨论】:

    • 我发布了代码。不,它不是子类,只是普通的 UITableViewCell。
    • 哦,好吧,那么您是如何将 UIButton 添加到单元格的?
    • 如果您以编程方式执行此操作,您可能需要检查一下:developer.apple.com/iphone/library/documentation/UserExperience/…
    • 我没有以编程方式进行,只是因为我的单元格中有很多不同的 UI 元素,我想在 IB 中布置它们。所以,我只是在 IB 中创建了一个 UITableViewCell,然后为它创建了一个 outlet,并为我想要使用它的单元格调用了该 outlet。
    • 查看这个链接,它会创建一个自定义的 NIB 并加载它:developer.apple.com/iphone/library/documentation/UserExperience/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多