繁体   English   中英

单元上的按钮未执行操作

[英]Button On Cell Not Performing Action

我在TableView Cell上有一个按钮。 我的问题是,单击单元格和按钮后,我们希望执行相同的操作。 在使用单元格选择方法单击单元格即时消息上。 他们单击单元格上的按钮时是否采取任何与单元格选择执行相同的操作的方法。

- (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];
    }

最好的办法是disable按钮的userInteractionEnabled ,这会将所有触摸都定向到UITableView's委托方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,您将能够执行该单元格触摸事件。

另外,如果根本不需要按钮事件,只需将按钮的userInteractionEnabled设置为YES然后添加以下代码即可-

内部单元格创建方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath add

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

添加实现事件为

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

}

cellForRowAtIndexPath ,您可以像下面这样编写此方法

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

可以帮助您,

享受编码!

如果您想获得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.
            }

您可以在UITableViewCellUIButton定义单独的方法。 如果要在单击UIButton the didSelectRowAtIndexPath上执行单独的操作。 请仔细检查代码,如何为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;

}

定义所选方法。

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