2010-04-26 73 views

回答

19
[myCellButton addTarget:self action:@selector(myCellButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
+0

我已经在UITableView的didSelectRowAtIndexPath方法中添加了此语句,但是当我触摸Button时它不在didSelectRowAtIndexPath中。如果我触摸按钮外部的UITableView的单元格/行,它将进入didSelectRowAtIndexPath。我不明白为什么会发生这种情况?.......如何解决这个问题? – 2010-04-26 05:45:02

+0

您将在创建'UIButton'后添加此目标,然后在单元初始化时将该按钮添加到单元。 'didSelectRowAtIndexPath'方法用于响应点击单元格(行)本身,而不是按钮。此目标操作是针对按钮本身的。这更清楚吗? – 2010-04-26 05:50:47

+0

我想在UITabelView的每个单元格上创建UIButton,当我点击该UIButton时,它应该改变UIButton上的图像。那么在哪里为每个单元格和后续代码创建UIButton,以便它可以在UITableView的每个单元格上为每个UIButton工作。 [myCellButton addTarget:self action:@selector(myCellButtonPressed :) forControlEvents:UIControlEventTouchUpInside]; – 2010-04-26 06:34:47

0

如果你正在做这一切的代码...

- (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子类。

相关问题