2014-07-22 42 views
-1

我有一個具有表格內容的類loginViewController。我已經將自定義單元格加載到表格的每一行。在方法cellForRowAtIndexPath我已經爲每一行創建一個按鈕,並賦予它一個標籤值。點擊這個按鈕將我們帶到新的視圖控制器logininsidesViewController。現在在logininsidesViewController類中,我想訪問我之前創建的按鈕標籤,我該怎麼做?訪問另一個類中的可用視圖單元格的內容

loginViewController.m類

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    customcell *cell=(customcell*)[tableView dequeueReusableCellWithIdentifier:@"cellcstm"]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button addTarget:self 
       action:@selector(customActionPressed) 
    forControlEvents:UIControlEventTouchUpInside]; 
    [button setBackgroundImage:[UIImage imageNamed:@"add_black.png"] forState:UIControlStateNormal]; 
    button.frame = CGRectMake(275 ,9, 25, 25); 
    [cell addSubview:button]; 

    if (cell==nil) 
    { 
     //[tableView registerNib:[UINib nibWithNibName:@"customcell" bundle:nil] forCellReuseIdentifier:@"cellcstm"]; 

     NSArray *arr12=[[NSBundle mainBundle]loadNibNamed:@"customcell" owner:self options:nil]; 
     cell=[arr12 objectAtIndex:0]; 
    } 
    for (int i=0; i<=indexPath.row; i++) 
    { 
     cell.selectionStyle=UITableViewCellEditingStyleNone; 
    } 
    if (indexPath.row==0) 
    { 
     cell.lbl.hidden=YES; 
     UILabel *l=[[UILabel alloc]init]; 
     [email protected]"Login Templates"; 
     l.frame=CGRectMake(100, 15, 180, 28); 

     [cell.contentView addSubview:l]; 
    } 

    if (indexPath.row==1) 
    { 
     button.tag=1; 
     cell.img.image=[UIImage imageNamed:@"facebook.png"]; 
     [email protected]"Facebook"; 
     [cell.contentView addSubview:button]; 
    } 
    if (indexPath.row==2) 
    { 
     button.tag=2; 
     cell.img.image=[UIImage imageNamed:@"g+.png"]; 
     [email protected]"Google"; 

     [cell.contentView addSubview:button]; 
    } 
    if (indexPath.row==3) 
    { 
     button.tag=3; 
     cell.img.image=[UIImage imageNamed:@"youtube.png"]; 
     [email protected]"Youtube"; 

     [cell.contentView addSubview:button]; 
    } 
    if (indexPath.row==4) 
    { 
     button.tag=4; 
     cell.img.image=[UIImage imageNamed:@"twitter.png"]; 
     [email protected]"Twitter"; 

     [cell.contentView addSubview:button]; 
    } 
    if (indexPath.row==5) 
    { 
     button.tag=5; 
     cell.img.image=[UIImage imageNamed:@"flicker.png"]; 
     [email protected]"Flicker"; 

     [cell.contentView addSubview:button]; 
    } 
    else 
    { 
     cell .textLabel.text= [logarr objectAtIndex:indexPath.row]; 
    } 
    return cell; 
} 

我想訪問button.tag我logininsidesViewController類。 plz幫助

回答

0

添加一個目標方法,以您的按鈕,並與標籤訪問它,

在cellForRow方法更新了這一點。

[button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside]; 

您可以通過標籤訪問這裏,

-(IBAction) customActionPressed:(id)sender{ 

    NSLog(@"button tag:%d",[sender tag]); 
} 
0

你加入你的button細胞,再要添加其對細胞的contentView正確這就是問題所在。

如果你想在cell.contentView加它,你必須在每個提供撥款if條件,然後將其添加

編輯:使用智能編碼

viewDidLoad() 



[self.myTableView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier]; 

    arImage = @[@"facebook.png", @"g+.png", @"twitter.png", @"flicker.png" ]; 
    arLables = @[@"Facebook", @"GooglePlus", @"Tweeter", @"Flicker"]; 

cellForRowAtIndexPath 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button addTarget:self action:@selector(customActionPressed) forControlEvents:UIControlEventTouchUpInside]; 
    [button setBackgroundImage:[UIImage imageNamed:@"add_black.png"] forState:UIControlStateNormal]; 
    button.frame = CGRectMake(275 ,9, 25, 25); 


    if (indexPath.row==0) 
    { 
     cell.lblText1.hidden=YES; 
     UILabel *l=[[UILabel alloc]init]; 
     [email protected]"Login Templates"; 
     l.frame=CGRectMake(100, 15, 180, 28); 

     [cell.contentView addSubview:l]; 
    } 



    if (indexPath.row > 0 && indexPath.row <= 5) { 
     button.tag = indexPath.row; 

     cell.imageView.image=[UIImage imageNamed:arImage[indexPath.row - 1]]; 
     cell.textLabel.text=[arLables objectAtIndex:indexPath.row - 1]; 

     [cell.contentView addSubview:button]; 

    } else { 
     cell.textLabel.text= [logarr objectAtIndex:indexPath.row]; 
    } 


    return cell; 
} 
1

在延續與Viruss的回答,您可以創建一個屬性在logininsidesViewController類中具有整數類型,並通過按鈕單擊。

-(IBAction) customActionPressed:(id)sender{ 

    NSLog(@"button tag:%d",[sender tag]); 
    logininsidesViewController *objLoginInside = [[logininsidesViewController alloc] initWithNibName:@"logininsidesViewController" bundle:nil]; 
    objLoginInside.buttonTag = sender.tag; 
    [self.navigationController pushViewController:objLoginInside animated:NO]; 

} 
相關問題