繁体   English   中英

UItableView单元格上的UIbutton不可单击

[英]UIbutton on UItableView Cell not clickable

我已经在uitable视图的单元格中添加了uibutton。 我希望按下此按钮并与用户互动。 我在单元实现类中添加了这一行代码。

readMore = [[UIButton alloc]initWithFrame: CGRectMake(180, 50, 67, 25)];
[readMore addTarget:self action:@selector(readMoreClic:) forControlEvents:UIControlEventTouchUpInside];
[readMore setImage:[UIImage imageNamed:@"readmore.png"] forState:UIControlStateNormal];      
[self addSubview:readMore];

问题在于它没有被单击(选择单元格后始终处于突出显示状态)。 我试过了 :-

1- [self setSelectionStyle:UITableViewCellSelectionStyleNone];
2- cell.userInteractionEnabled = NO

但我仍然选择了单元格,并用蓝色突出显示了该单元格,并且未单击按钮。 即使我选择了

[self setSelectionStyle:UITableViewCellSelectionStyleGray]

有人帮忙

[请注意我在同一单元格中有一个textView,它允许交互(选择和复制)] [我正在将xcode 4与iOS 4.3一起使用]

尝试其他控制事件?:

[readMore addTarget:self action:@selector(readMoreClic:) forControlEvents:UIControlEventTouchDown];

好吧,我不太明白为什么您的按钮不响应,因为我遇到过类似的问题,但是我使用了不同的解决方案。 这是我所做的:

CGRect reuseableRect = CGRectMake(20, 290, 260, 37);

self.actionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.actionButton.frame = reuseableRect;

[self.actionButton setTitle:@"Initialize" forState:UIControlStateNormal];
[self.actionButton addTarget:nil action:@selector(performAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.actionButton];

实际上,我这样做是:

self.actionButton = [[UIButton alloc] initWithFrame:reuseableRect];

但这没用。 所以我把它改成了

self.actionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

resueableRect只是我在视图初始化中一直使用的CGRect。

至于您先前解决方案的三行,第一行:

[self setSelectionStyle:UITableViewCellSelectionStyleNone];

它正在对tableView及其单元格进行一些配置,因此不会影响按钮

第二个:

cell.userInteractionEnabled = NO;

它也没有配置按钮,而且,由于单元格是按钮的超级视图,因此该行可防止任何用户交互。

第三个也没有对按钮做任何事情,因此如果您正在考虑配置按钮,我建议您删除所有这些按钮

您无需使用选择样式或userInteractionEnabled进行任何操作。 确保readMoreClic:看起来像:

-(void)readMoreClic:(id)sender
{
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM