UITableViewCell中的UIButton没有调用该操作

时间:2011-04-06 05:24:35

标签: iphone uitableview uibutton

我在UITableViewCell中有一个UIButton,我想在它被选中时切换它的图像。以下是我的代码:

- (UITableViewCell *)tableView:(UITableView *)todoTable cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell = [self.todoTable dequeueReusableCellWithIdentifier:CellIdentifier];

if(!cell)
    cell = [self getCellContentView:CellIdentifier];


UIImageView *checkboxView = (UIImageView *)[cell viewWithTag:1];

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 30.0, 30.0)];

UIImage *uncheckedButtonImage = [UIImage imageNamed:@"unchecked_checkbox.png"];
[button setImage:uncheckedButtonImage forState:UIControlStateNormal];

[button addTarget:self action: @selector(toggleImage:) forControlEvents: UIControlEventTouchUpInside];

[checkboxView addSubview:button];

}`

我的toggleImage方法只是: - (void)toggleImage { NSLog(@"test"); }

但是每当我点击按钮本身时,我都不想在didSelectRowAtIndexPath调用单元格行,我希望按钮调用toggleImage方法。是我用错误的方式设置按钮的动作吗?

感谢。

2 个答案:

答案 0 :(得分:1)

将此- (void)toggleImage { NSLog(@"test"); }更改为

- (void)toggleImage:(id)sender { NSLog(@"test"); }并查看它是否有效。

答案 1 :(得分:0)

7KV7是正确的,您需要将按钮对象发送到该方法以更改其图像。

看一下这个帖子

how-to-select-particular-check-box-in-tableview-which-is-inserted-in-table-cell

相关问题