自定义UITableViewCell didSelectRowAtIndexPath未被调用

时间:2014-03-20 22:30:33

标签: ios objective-c uitableview uiscrollview

我创建了一个自定义的UITableViewCell类,用于绘制我的UITableViewCell。一切都是正确绘制的,但是由于我放入UITableViewCell的元素,我在选择单元格方面遇到了问题。

这是我用来绘制UITableViewCell的方法,这是我的自定义UITableViewCell

- (void)drawCell
{
    nameString = [[UILabel alloc] init];
    nameString.backgroundColor = [UIColor redColor];
    nameString.frame = CGRectMake(15.0, 0.5, 70.0, 40.0);
    nameString.text = [itemsDictionary objectForKey:@"Name"];

    lastNameString = [[UILabel alloc] init];
    lastNameString.backgroundColor = [UIColor clearColor];
    lastNameString.frame = CGRectMake(105.0, 0.5, 95.0, 40.0);
    lastNameString.text = [itemsDictionary objectForKey:@"LastName"];

    addressString = [[UILabel alloc] init];
    addressString.backgroundColor = [UIColor clearColor];
    addressString.frame = CGRectMake(220.0, 10.5, addressString.frame.size.width, 50.0);
    addressString.text = [NSString stringWithFormat:@"ISN %@: %@",[itemsDictionary objectForKey:@"AddNumber"] ,[itemsDictionary objectForKey:@"AddString"]];
    [addressString sizeToFit];

// scrollcell has a dynamic scrollwidth depending on the sddressString but has a framesize of a normal UITableViewCell
    scrollCell = [[UIScrollView alloc] init];
    scrollCell.backgroundColor = [UIColor blueColor];
    scrollCell.frame = CGRectMake(0.0, 0.0, ScreenWidth, 45.0);
    [scrollCell setContentSize:(CGSizeMake((220.0 + addressString.frame.size.width)+15, 45.0))];

    [scrollCell addSubview:nameString];
    [scrollCell addSubview:lastNameString];
    [scrollCell addSubview:addressString];
    [[self contentView] addSubview:scrollCell];
}

正如您所看到的,我正在添加一个覆盖整个单元格的UIScrollView,我认为它阻止了UITableViewCell委托选择方法。

如何让委托方法didSelectRowAtIndexPath起作用?

3 个答案:

答案 0 :(得分:1)

您是否添加了以下内容?

@implementation ViewController <UITableViewDelegate, UITableViewDataSource>

并将委托设置为自己?

self.tableview.delegate = self;

答案 1 :(得分:0)

单元格中的UIScrollView总是会成为一个问题,因为它会拦截事件。如果可以,请依赖于表视图滚动并使单元格显示内容所需的大小。

如果你必须有滚动视图,你可能需要在单元格的顶部添加一个视图,添加自己的手势识别器,并选择将哪些事件发送到表格视图以及哪些事件发送到滚动视图。< / p>

还可以查看this answer - 您也可以将touchesBegan / Moved / Ended发送到nextResponder。

答案 2 :(得分:0)

另外两个答案中的一个可能是正确的路径,但是如果您使用的是故事板segue,那么在didSelectRowAtIndexPath之前会有这样的代码,因此您在那里的代码可能会根据segue和发生的情况而无关紧要。

相关问题