单元格上的按钮不执行操作

时间:2015-04-17 04:53:49

标签: ios objective-c

我在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];
    }

4 个答案:

答案 0 :(得分:1)

最好的方法是disable按钮的userInteractionEnabled,它会将所有触摸指向UITableView's委托方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath并且您将能够执行细胞触摸事件。

或者,如果您需要按钮事件而不是将其userInteractionEnabled设置为YES并添加以下代码 -

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

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

将实施事件添加为

- (void) cellButtonTouched:(id)sender
{

}

答案 1 :(得分:1)

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
}

可以帮助你,

享受编码!!

答案 2 :(得分:1)

如果你想得到UIButton的动作只是复制过你的代码并且它正在工作。但是如果你想禁用UIButton只需按钮.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.
            }

答案 3 :(得分:1)

您可以在UIButton中为UITableViewCell定义单独的方法。如果您想点击UIButton 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);
}