簡體   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