如何检测表格视图单元格中的多个按钮

How to detect multiple buttons in tableview cell

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

使用部分和行的组合为每个按钮分配标签。

当分配给按钮的方法被调用时,使用“%”和“/”进行进一步操作。

如果您在实施过程中遇到任何困难,请告诉我。

进入您的 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的作用是识别用户选择的是哪个cell。

然后转到 CustomCell.m & 在您的按钮 IBAction 方法中添加此代码。

[self.delegate buttonActionwith:self.buttonIndexPath];

这行代码的意思是,当用户TouchUpInside的Button中的cell中的buttonActionwith委托方法调用时,如果你在UITableView所在的UIViewController设置了这个CustomCellDelegate,那个委托方法也会在 ViewController 内部调用。

现在转到您的 MainViewController.h 并添加 自定义单元格委托 & 之后它看起来像这样,

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

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

//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);
}

这是在 Cell 中添加单个按钮的方法。您可以使用此方法将更多按钮添加到单元格。

我喜欢这个:

- (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

中定义它

P.S - 根据需要设置按钮 CGRectMake