UITableView自定义单元格根本无法被选中

3

我创建了一个自定义的tableView单元格,我可以看到我的tableView使用了期望的自定义单元格。现在的问题是我不能选择我的表格行。我尝试从- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath中显示控制台输出,但它从未被调用过。我很困惑。我不知道我做错了什么。下面是我的自定义单元格类:

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

        // Initialization code

        CGRect contentRect = self.contentView.bounds;
        CGFloat boundsX = contentRect.origin.x;

        _title = [[UILabel alloc]initWithFrame:CGRectMake(boundsX +70, 5, 100, 25)];
        _title.textAlignment = UITextAlignmentLeft;
        _title.font = [UIFont systemFontOfSize:14];

        _details = [[UILabel alloc]initWithFrame:CGRectMake(boundsX + 70, 30, 100, 15)];
        _details.textAlignment = UITextAlignmentLeft;
        _details.font = [UIFont systemFontOfSize:10];

        _duration = [[UILabel alloc] initWithFrame:CGRectMake(boundsX + 200, 10, 65, 15)];
        _duration.textAlignment = UITextAlignmentRight;
        _duration.font = [UIFont systemFontOfSize:10];

        _price = [[UILabel alloc]initWithFrame:CGRectMake(boundsX + 180, 30, 80, 15)];
        _price.textAlignment = UITextAlignmentRight;
        _price.font = [UIFont systemFontOfSize:10];

        _listingImage = [[UIImageView alloc]initWithFrame:CGRectMake(boundsX +10, 0, 50, 50)];

        [self.contentView addSubview:_title];
        [self.contentView addSubview:_details];
        [self.contentView addSubview:_price];
        [self.contentView addSubview:_duration];
        [self.contentView addSubview:_listingImage];

        [_title release];
        [_details release];
        [_duration release];
        [_price release];
        [_listingImage release];

    }

    return self;

}

以下是我的tableView: cellForRowAtIndexPath:函数:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    CustomizedCell *cell = (CustomizedCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[CustomizedCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
     //   cell = [[[CustomizedCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];

    }

    ADJListing *listing = [self.dataSource.listings objectAtIndex:indexPath.row];

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;


    cell._title.text = listing.title;
    cell._details.text = listing.details;
    cell._price.text = [listing.price stringValue];
    cell._duration.text = (NSString*) listing.start_datetime;
    cell._listingImage.image = [UIImage imageNamed:@"picture.png"];

    return cell;
}

再次分享我的tableView:didSelectRowAtIndexPath:方法。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"data");

    detailedVC = [[DetailedViewController alloc] init];
    detailedVC.dataSource = self.dataSource;
    [self.navigationController pushViewController:detailedVC animated:YES];
    [detailedVC release];
}

任何帮助都将不胜感激。
谢谢, Vik
3个回答

1

我明白了。我不知怎么把self.tableView.allowsSelection = NO,而且我没注意到它。现在它正常工作了。谢谢


很奇怪,我也遇到了同样的问题。在对XIB文件进行一些操作后,我不得不明确地设置这个属性,即使它默认为YES! - nurnachman

0

嗨,请检查xib文件。 进入.xib,选择您的视图,在右侧面板中检查“启用用户交互”。


0

在执行完didSelectRowAtIndexPath方法的其他代码后,尝试取消选中行。

另外,你是否已将你的UITableViewController子类声明为tableview的代理?最简单的方法是在IB中完成这个操作。如果这不是UITableViewController子类,则需要在头文件中声明并实现UITableViewDelegate。


嗨,W Dyson。感谢您的回复。我也尝试了,但没有改变。它还是一个UIViewController子类,我已经在头文件中声明了UITableViewDelegate,并且我已将tableView的代理设置为self。 - Vik
大家好。我搞定了。我不知怎么把self.tableView.allowsSelection = NO,错过了它。现在它正常工作了。谢谢。 - Vik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接