iOS:自定义UITableViewCell触摸接收器混乱

时间:2011-04-27 11:16:40

标签: iphone uitableview interface-builder

已经看到类似的问题,但找不到明确的答案。

掌握了大多数类型的常规表后,我正在使用自定义表格单元进行一些概念性实验,以熟悉其工作原理。我想有一个UITableViewCell的自定义子类,它将一个nib加载到contentView。我可能希望稍后实现不同的编辑样式,但是想在我的应用程序的不同部分重用自定义视图,但是,我在调用UITableViewController时遇到didSelectRowAtIndexPath消息时遇到问题。

这是从基本视图模板构建的层次结构。

CustomCellViewController:从UITableViewCell中获取的股票XCode objective-c类

@interface CustomCellViewController : UITableViewCell {
  IBOutlet UILabel *lbl;
}

@property (nonatomic, retain) IBOutlet UILabel *lbl;

@end

@implementation CustomCellViewController

@synthesize lbl;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        NSArray *a = [[NSBundle mainBundle] loadNibNamed:@"customCellView" owner:self options:nil];
        UITableViewCell *tc = [a objectAtIndex:0];
        NSLog(@"Cell loaded from nib");
        [self.contentView addSubview:tc];
    }
    return self;
}

.. the other stock methods are unchanged ..

@end

我意识到init方法可以简化,但这是我的第一次尝试。

XIB文件的所有者是我的自定义类(CustomCellViewController),有一个UITableViewCell和一个标签(链接到它上面的插座'lbl')位于中间位置,留下了大量底层的UITableViewCell可点击。

RootViewController是UITableViewController的标准库存XCode子类 RootViewController设置一个实例变量“CustomTableCellController * myCustomCell”

cellForRowAtIndexPath:如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"myCustomCell";

    CustomCellViewController *cell = (CustomCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {  
        cell = [[[CustomCellViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        NSLog(@"Creating New cell for position %d", indexPath.row);
    } else {
        NSLog(@"Reusing cell for position %d", indexPath.row);
    }

    // Configure the cell.
    cell.lbl.text = [NSString stringWithFormat: @"Hi There %d", indexPath.row];
    return cell;
}

在同一个RootViewController.m中,我的didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Cell tapped at row %d", indexPath.row);
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}

目前设计,在点击时纯粹输出日志信息。

numberOfSectionsInTableView返回1 numberOfRowsInSection返回50

这一切都编译并运行良好,iPhone模拟器启动,我在显示器上看到一个表,我的输出日志确认它已经创建了9个版本的CustomCellViewController,我可以通过NSLog()注释看到重用堆栈。

我只是无法选择一行,无论我在自定义单元格中单击的位置都没关系,没有任何内容可以访问我的didSelectRowAtIndexPath:在我所期望的RootViewController中。

我有没有在某个地方设置代表,如果是的话,怎么样?我是否需要通过急救员做到这一点? (即,在我的CustomCellViewController类中创建一个方法,将UITableViewCell从XIB链接到该方法,然后调用[super didSelectRowAtIndexPath] - 但是如何传递indexPath?

我是否没有回复来自我的XIB的消息,然后将其传递给我(这是我怎么做的?)

我通读了所有的苹果文档以进入这个阶段,但无法完全解读触摸消息的发生方式。

有点困惑!

1 个答案:

答案 0 :(得分:1)

如果tableview对象将其设置为如下所示,则可能忘记设置数据源和委托

tbl.delegate = self;
tbl.dataSource = self;