2015-04-17 55 views
-1

我在TableView單元上有一個按鈕。我的問題是,在點擊單元格和按鈕時,我們想要執行相同的操作。點擊一個單元我使用做選擇方法。他們的任何方法是點擊單元格上的按鈕,他們執行與單元格選擇執行相同的操作。單元格上的按鈕不執行操作

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

你可以發佈你的代碼更好的通關你做了什麼? @varun – iPeter

+3

只需設置'button.userInteractionEnabled = NO',didSelected將被解僱,如果你想這樣做 –

+0

thnks bddy它的工作 – varun

回答

1

的最好的事情是disable按鈕的userInteractionEnabled,這將直接所有接觸到UITableView's委託方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,你將能夠執行小區觸摸事件。

另外,如果在所有你需要有按鈕的事件不是設置其userInteractionEnabledYES並做加以下代碼 -

細胞內創建方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 添加

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

添加實施事件爲

- (void) cellButtonTouched:(id)sender 
{ 

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

月幫助你,

享受編碼!

1

如果你想獲得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. 
      } 
1

可以在UITableViewCell定義UIButton單獨的方法。如果您想單擊UIButtondidSelectRowAtIndexPath單獨執行操作。請仔細閱讀代碼,瞭解如何爲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); 
} 
相關問題