[英]How to handle touch event on UIButton added on cell of UITableView?
我动态地在UITableView的每个单元格上添加UIButton。 我想在这个UIButton上处理触摸事件。 你能帮我解决一下这个问题吗?
在此先感谢Vikas
[myCellButton addTarget:self action:@selector(myCellButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
如果你在代码中完成所有操作......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(cellButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button sizeToFit];
[cell.contentView addSubview:button];
}
...
return cell;
}
...
- (IBAction)cellButtonPressed:(id)sender
{
[sender setImage:[UIImage imageNamed:@"newImage"] forState:UIControlStateNormal];
}
但我建议使用带委托的UITableViewCell子类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.