Ios 未在自定义单元格标签内的UIbutton中获取操作
我有以下设置:我有UITableView,我正在其中设置名为PBDetailsCell的自定义单元格 PBDetailsCell包含2个标签:LeftLabel和RightLabel。我正在右标签内设置一个按钮,当在PBDetailsCell的右标签中按下该按钮时,我希望它调用在UITableView中设置的buttonPressed:方法 我目前正在写这段代码,但似乎按下的按钮没有被调用Ios 未在自定义单元格标签内的UIbutton中获取操作,ios,uitableview,uibutton,Ios,Uitableview,Uibutton,我有以下设置:我有UITableView,我正在其中设置名为PBDetailsCell的自定义单元格 PBDetailsCell包含2个标签:LeftLabel和RightLabel。我正在右标签内设置一个按钮,当在PBDetailsCell的右标签中按下该按钮时,我希望它调用在UITableView中设置的buttonPressed:方法 我目前正在写这段代码,但似乎按下的按钮没有被调用 - (UITableViewCell *)tableView:(UITableView *)tableVi
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ PBDetailsCell *cell = (PBDetailsCell *) [tableView dequeueReusableCellWithIdentifier:[PBDetailsCell reuseIdentifier]];
if(cell==nil){
[[NSBundle mainBundle] loadNibNamed:@"PBDetailsCell" owner:self options:nil];
cell=_detailCell;
_detailCell=nil;
}
cell.leftLabel.text = @" Action";
UIImage* img = [UIImage imageNamed:@"action.png"];
UIButton* but = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, cell.rightLabel.frame.size.width-20, cell.rightLabel.frame.size.height-10)];
[but addTarget:self action:@selector(btnPressed:) forControlEvents: UIControlEventTouchUpInside];
[but setImage:img forState:UIControlStateNormal];
[cell.rightLabel addSubview:but];
}
- (void)btnPressed:(id)sender
{ NSLog(@"Button pressed");}
更换这条线
[but addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchDown];
与
如果使用自定义单元格,则使用UITableCell中的按钮静态,并在xib中添加按钮和标签,并连接按钮的方法。事件必须是
UIControlEventTouchUpInside
。同时尝试将单元格选择样式设置为None
。可能是细胞得到了触碰
cell.selectionStyle=UITableViewCellSelectionStyleNone
试试这个
将标签和按钮的
setUserInteractionEnabled
设置为yes
我想通过编程来实现,因为我还在uitableview中的其他行中使用相同的自定义单元格,并且我只在一行中使用按钮…[\u detailCell.rightLabel addSubview:but];请尝试此操作并正确检查。请稍候,我将签入演示项目。是,cell.rightLabel.userInteractionEnabled=Yes;做了诀窍:)愚蠢的解决方案:添加cell.rightLabel.userInteractionEnabled=YES;对代码的修改将允许我选择按钮。实际上,我认为愚蠢的解决方案是在UILabel中放置UIButton。我想不出为什么需要这个。哈哈。。根据应用程序设计,这是必需的。。我对此无能为力。。。
[but addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];