简体   繁体   中英

Button On Cell Not Performing Action

I have a button on TableView Cell. My Issue is that on clicking on cell and button we want to perform same action. on clicking a cell im using did select method. Is their any method on clicking a button on cell they perform same action as cell did select perform.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%ld CustumCell",(long)indexPath.row];
    CustumCell *cell = (CustumCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
        cell = [nib objectAtIndex:2];
        UIButton *button;
        UIImageView *imageView;
        UILabel *nameLabel;

        if (screenWidth ==320) {
            imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width-40, cell.contentView.frame.size.height+47)];
            button=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-40, 0, 40,  cell.contentView.frame.size.height+47)];
            nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2-40, (cell.contentView.frame.size.height+20)/2, 100, 30)];
        }

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        UIImageView *images = (UIImageView *)[cell.contentView viewWithTag:100];
        images.image=[UIImage imageNamed:[tableImageArray objectAtIndex:indexPath.row]];
         UIButton *buttond = (UIButton *)[cell.contentView viewWithTag:101];
        [buttond setImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];
        UILabel *label=(UILabel *)[cell.contentView viewWithTag:102];
        label.text=[cellNameArray objectAtIndex:indexPath.row];
        label.textColor =[UIColor whiteColor];

    }

    return cell;
}

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


        table.separatorStyle = UITableViewCellSeparatorStyleNone;
        table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        SelectVoucher *selectVoucher=[[SelectVoucher alloc]initWithNibName:@"SelectVoucher" bundle:nil];
        selectVoucher.hotelName =[cellNameArray objectAtIndex:indexPath.row];
        [self presentViewController:selectVoucher animated:YES completion:nil];
    }

The best thing is to disable the userInteractionEnabled of the button, which will direct all the touches to the UITableView's delegate method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath and you will be able to perform the cell touch events.

Alternatively, if at all you need to have the event of the button than set its userInteractionEnabled to YES and do add following code-

Inside cell creation method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath add

[buttond addTarget:self action:@selector(cellButtonTouched:) forControlEvents:UIControlEventTouchUpInside];

add implement the event as

- (void) cellButtonTouched:(id)sender
{

}
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
cell = [nib objectAtIndex:2];
UIButton *button;
UIImageView *imageView;
UILabel *nameLabel;

if (screenWidth ==320) {
    imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width-40, cell.contentView.frame.size.height+47)];
    button=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-40, 0, 40,  cell.contentView.frame.size.height+47)];
    nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2-40, (cell.contentView.frame.size.height+20)/2, 100, 30)];
}

cell.selectionStyle=UITableViewCellSelectionStyleNone;

UIImageView *images = (UIImageView *)[cell.contentView viewWithTag:100];
images.image=[UIImage imageNamed:[tableImageArray objectAtIndex:indexPath.row]];
 UIButton *buttond = (UIButton *)[cell.contentView viewWithTag:101];
[buttond setImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];

//This line you need to add
[buttond addTarget:self action:@selector(YourMethodtocall:) forControlEvents:UIControlEventTouchDown];**
/////
UILabel *label=(UILabel *)[cell.contentView viewWithTag:102];
label.text=[cellNameArray objectAtIndex:indexPath.row];
label.textColor =[UIColor whiteColor];

}

And out side of cellForRowAtIndexPath , you can write this method like

-(IBAction)YourMethodtocall:(id)sender {
    //your code here to perform action
}

May helps you,

Enjoy Coding !!

If you want to get the action of the UIButton that just copy past your code and it's working.But if you want to disable the UIButton so just button.userInteractionEnabled=NO and then access the didSelected Method.

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
            {

                NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%ld CustumCell",(long)indexPath.row];
                CustumCell *cell = (CustumCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

                if (cell == nil)
                {
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
                    cell = [nib objectAtIndex:2];
                    UIButton *button;
                    UIImageView *imageView;
                    UILabel *nameLabel;

                    if (screenWidth ==320) {
                        imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width-40, cell.contentView.frame.size.height+47)];
                        button=[[UIButton alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-40, 0, 40,  cell.contentView.frame.size.height+47)];
                        nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width/2-40, (cell.contentView.frame.size.height+20)/2, 100, 30)];
                    }

                    cell.selectionStyle=UITableViewCellSelectionStyleNone;

                    UIImageView *images = (UIImageView *)[cell.contentView viewWithTag:100];
                    images.image=[UIImage imageNamed:[tableImageArray objectAtIndex:indexPath.row]];

                     UIButton *buttond = (UIButton *)[cell.contentView viewWithTag:101];
                    [buttond setImage:[UIImage imageNamed:@"plus.png"] forState:UIControlStateNormal];
                    [buttond addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
                    [buttond setTitle:[NSString stringWithFormat:@"%li %li",(long)indexPath.section,(long)indexPath.row] forState:UIControlStateDisabled];

                    UILabel *label=(UILabel *)[cell.contentView viewWithTag:102];
                    label.text=[cellNameArray objectAtIndex:indexPath.row];
                    label.textColor =[UIColor whiteColor];

                }

                return cell;
}

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


                    table.separatorStyle = UITableViewCellSeparatorStyleNone;
                    table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
                    SelectVoucher *selectVoucher=[[SelectVoucher alloc]initWithNibName:@"SelectVoucher" bundle:nil];
                    selectVoucher.hotelName =[cellNameArray objectAtIndex:indexPath.row];
                    [self presentViewController:selectVoucher animated:YES completion:nil];
                }

        -(void)btnAction:(UIButton*)sender{
                NSString *str=[sender titleForState:UIControlStateDisabled];
                NSArray *ar=[str componentsSeparatedByString:@" "];
                NSIndexPath *indexPath=[NSIndexPath indexPathForRow:[[ar objectAtIndex:1] intValue] inSection:[[ar objectAtIndex:0] intValue]];
                btnSelectedSignOut= sender;
                NSLog(@"Value from sign out action %@",indexPath);

        //here is the indexpath value you can do any logic here.
            }

You can define separate method for UIButton in UITableViewCell . if you want to perform separate action on clicking UIButton the didSelectRowAtIndexPath . kindly look over the code how to define separate action for UIButton .

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   CellIdentifier];
NSInteger index = indexPath.row;

if (cell == nil) {

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"cell"owner:self options:nil];
    cell = [nib objectAtIndex:0];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.tag=indexPath.row;
   [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
   [button setTitle:@"cellButton" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 0.0, 160.0, 40.0);
   [cell.contentView addSubview:button];

}

return cell;

}

To define selected method.

-(void)aMethod:(UIButton*)sender
{
    NSLog(@"I Clicked a button %d",sender.tag);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM