How to detect multiple buttons in tableview cell

喜欢而已 提交于 2019-12-11 01:52:32

问题


How to detect multiple buttons in tableview cell and my doubt is with example i have 3 buttons in cell if i tap on one button that button will change colour and and if i click indexpath.row=1 cell button that button will color also need to change help me


回答1:


I did like this:

- (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 is an Int. In your case define it in viewDidLoad total_buttons_in_a_row=3

P.S - set the Buttons CGRectMake according to your need.




回答2:


Assign tag to each button using the combination of section & row..

When method assigned to button get called use '%' & '/' for further manipulation.

Let me know if you have any difficulty in implementing it..




回答3:


Got to your CustomCell.h & right below #import add this code.

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

Then Add this code right below @interface CustomCell : UITableViewCell

//Manual Properties
@property (strong, nonatomic) NSIndexPath *buttonIndexPath;

//Delegate
@property (nonatomic, weak) id <CustomCellDelegate> delegate;

The use of this NSIndexPath is to identify which cell user selecting.

Then go to CustomCell.m & in your button IBAction method add this code.

[self.delegate buttonActionwith:self.buttonIndexPath];

The meaning of this line of code is that, when the user TouchUpInside the Button in the cell buttonActionwith delegate method calls, if you set this CustomCellDelegate in your UIViewController where the UITableView is, that delegate method will call inside that ViewController too.

Now go to your MainViewController.h & add CustomCellDelegate & it will look like this after that,

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

when creating the Custom UITableViewCell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath don't forget to add this line of code before return cell;

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

The modified - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath will look like this,

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;

Finally add this method inside MainViewController.m

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

This is how to add a single button inside the Cell. you can use this method to more buttons to the cell.



来源:https://stackoverflow.com/questions/29964147/how-to-detect-multiple-buttons-in-tableview-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!