来自UITutViewCell内的UIButton的IBAction

时间:2010-08-26 22:38:56

标签: iphone uitableview ibaction

我在Interface Builder中创建了一个UITableViewCell,我已经添加了一个UIImageView和一个UIButton。我已经给了UIButton一个插座,并将它连接到IBAction,并在事件集中进行了修饰。我以为它会调用这个方法,因为它看起来像是连接起来了:

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

3 个答案:

答案 0 :(得分: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返回一个值,所以它导致了我所有其他单元格的问题

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

相关问题