如何检测tableview单元格中的多个button

如何检测tableview单元格中的多个button,我的疑问是与例如我有3个button单元格中,如果我点击一个button,该button将改变颜色,如果我点击indexpath.row = 1单元格button,该button将颜色也需要改变帮助我

我这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; } UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(10.0, 0.0, self.tableView.frame.size.width/4, 40.0); button.tag = 100 + indexPath.row*total_buttons_in_a_row; [button setTitle:[NSString stringWithFormat:@"%ld",(long)button.tag] forState:UIControlStateNormal]; [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [cell addSubview:button]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button2.frame = CGRectMake(10.0+self.tableView.frame.size.width/4+10.0, 0.0, self.tableView.frame.size.width/4, 40.0); button2.tag = 100 + indexPath.row*total_buttons_in_a_row + 1; [button2 setTitle:[NSString stringWithFormat:@"%ld",(long)button2.tag] forState:UIControlStateNormal]; [button2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [cell addSubview:button2]; UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button3.frame = CGRectMake(10.0+self.tableView.frame.size.width/4*2+10.0, 0.0, self.tableView.frame.size.width/4, 40.0); button3.tag = 100 + indexPath.row*total_buttons_in_a_row + 2; [button3 setTitle:[NSString stringWithFormat:@"%ld",(long)button3.tag] forState:UIControlStateNormal]; [button3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [cell addSubview:button3]; return cell; } -(void)btnClicked:(UIButton *)sender{ id selectedButton = [self.view viewWithTag:sender.tag]; if ([selectedButton backgroundColor] == [UIColor redColor]) { [selectedButton setBackgroundColor:[UIColor clearColor]]; }else{ [selectedButton setBackgroundColor:[UIColor redColor]]; } } 

total_buttons_in_a_row是一个Int 。 在你的情况下定义它在viewDidLoad total_buttons_in_a_row=3

PS – 设置buttonCGRectMake根据您的需要。

使用部分和行的组合将标签分配给每个button..

分配给buttonget的方法调用时使用'%'&'/'进一步操作。

让我知道如果你有任何困难实施它..

到了你的CustomCell.h&#在下面#import添加这个代码。

 @protocol CustomCellDelegate <NSObject> - (void)buttonActionwith :(NSIndexPath *)indexPath; @end 

然后在@interface CustomCell:UITableViewCell下面添加这个代码

 //Manual Properties @property (strong, nonatomic) NSIndexPath *buttonIndexPath; //Delegate @property (nonatomic, weak) id <CustomCellDelegate> delegate; 

这个NSIndexPath的使用是识别哪个单元用户select。

然后去CustomCell.m&在你的buttonIBAction方法添加这个代码。

 [self.delegate buttonActionwith:self.buttonIndexPath]; 

这行代码的含义是,当用户TouchUpInside中Button的buttonAction用委托方法调用时,如果在你的UITableView的UIViewController中设置了这个CustomCellDelegate,那么这个委托方法也会在该ViewController中调用。

现在去你的MainViewController.h&添加CustomCellDelegate它看起来像这样,

 @interface MainViewController : UIViewController <CustomCellDelegate,,UITableViewDataSource,UITableViewDelegate> 

当在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath创build自定义UITableViewCell时, - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不要忘记在返回单元格之前添加这行代码;

 //Set Cell Values Here cell.delegate = self; //Setting delegate to self cell.buttonIndexPath = indexPath; 

修改后的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath将如下所示,

 static NSString *MyIdentifier = @"MyIdentifier"; //Set Identifier for cell CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; //init CustomCell if (cell == nil) { //Check cell is nill or not NSArray *nib; nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; //if cell is nil add CustomCell Xib for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]]) cell = (CustomCell *)oneObject; } //Set Cell Values Here cell.buttonIndexPath = indexPath return cell; 

最后在MainViewController.m中添加这个方法

 #pragma mark - SwipeableCellDelegate - (void)buttonActionwith:(NSIndexPath *)indexPath { //Delegate method NSLog(@"Button Clicks at index %ld",(long)indexPath.row); } 

这是如何在单元格内添加一个button。 你可以使用这个方法来更多的单元格button。