2

我在 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;
}
4

3 回答 3

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 的值,所以它导致了我所有其他单元格的问题

于 2010-08-26T23:55:19.373 回答
0

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

于 2010-08-26T22:42:43.813 回答
0

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

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

于 2010-08-26T22:43:57.897 回答