0

我在我的视图中使用了三个表,并使用标签将它们分开。在我的第二个表格视图中,我制作了一个自定义 uitableview 并在其中添加标签和按钮。

这是我在 cellforrowatindexpath 中写的按钮

followingButton = [UIButton buttonWithType:UIButtonTypeCustom];

  [followingButton addTarget:self action:@selector(followingButtonpressed:)forControlEvents:UIControlEventTouchUpInside];

 [followingButton setImage:[UIImage imageNamed:@"following12.png"] forState:UIControlStateNormal];

following.tag=indexpath.row;

 [followingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

followingButton.frame = CGRectMake(220.0 ,20.0, 100, 40.0);

 [cell.contentView addSubview:followingButton];

======

-(void)followingButtonpressed:(id)sender
{

 UIView *contentView1 = (UIView *)[sender superview];

 UITableViewCell *cell1= (UITableViewCell *)[contentView1 superview];

 NSIndexPath *indexPath1 = [followingTable indexPathForCell:cell1];

[sender tag];

NSLog(@"sender tag --%d",[sender tag]);

 // UIButton *button = (UIButton *)sender;

 // UITableViewCell *cell = (UITableViewCell*)[button superview];

  UIButton *tempButtom = (UIButton *)[cell1 viewWithTag:[sender tag]];

  [tempButtom setImage:[UIImage imageNamed:@"following_off12.png"] 

forState:UIControlStateNormal];

}

但是它崩溃了,我无法更改所选按钮的图像,请帮助

4

2 回答 2

0
Use this to detect which button has been pressed

    UIView *senderButton = (UIView*) sender;
    NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];
于 2013-01-18T09:53:24.220 回答
0

Tag每个UIButton并编写以下代码以检测按下哪个按钮

 - (void)buttonPressedAction:(id)sender
{
   UIButton *button = (UIButton *)sender;
   // Get the UITableViewCell which is the superview of the UITableViewCellContentView which is the superview of the UIButton
   (UITableViewCell*)cell = [[button superview] superview];
   int row = [myTable indexPathForCell:cell].row;

  // Here **row** give you tad of specific UIButton that you tapped

  if (row == yourTag )
  {
     // write code for action
  }
}

或者您可以每次为单元格设置标签,但不能设置 UIButton。您的操作将如下所示:

- (void)buttonPressedAction:(id)sender
{ 
   UIButton *button = (UIButton *)sender;
   int row = [button superview].tag;

   // Here **row** give you tad of specific UIButton that you tapped

  if (row == yourTag )
  {
     // write code for action
  }

}
于 2013-01-18T10:00:48.543 回答