简体   繁体   English

单元上的按钮未执行操作

[英]Button On Cell Not Performing Action

I have a button on TableView Cell. 我在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. 最好的办法是disable按钮的userInteractionEnabled ,这会将所有触摸都定向到UITableView's委托方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,您将能够执行该单元格触摸事件。

Alternatively, if at all you need to have the event of the button than set its userInteractionEnabled to YES and do add following code- 另外,如果根本不需要按钮事件,只需将按钮的userInteractionEnabled设置为YES然后添加以下代码即可-

Inside cell creation method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath add 内部单元格创建方法- (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 cellForRowAtIndexPath ,您可以像下面这样编写此方法

-(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. 如果您想获得UIButton的操作,而该操作只是复制您的代码即可,那么它就可以工作。但是,如果您要禁用UIButton,则只需将button.userInteractionEnabled = NO禁用,然后访问didSelected方法即可。

  - (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 . 您可以在UITableViewCellUIButton定义单独的方法。 if you want to perform separate action on clicking UIButton the didSelectRowAtIndexPath . 如果要在单击UIButton the didSelectRowAtIndexPath上执行单独的操作。 kindly look over the code how to define separate action for UIButton . 请仔细检查代码,如何为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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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