2011-10-22 85 views
0

我是一個新的程序員的我的手機通按鈕兩個參數點擊(訪問按鈕點擊兩個字符串)

我有一個小問題....我想傳遞兩個參數按鈕點擊....

//---insert individual row into the table view--- 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease]; 

    } 


    cell.selectionStyle = UITableViewCellSelectionStyleNone;//my code.... change the color or selected cell 



    search_items *pro = [searchCount objectAtIndex:[indexPath row]]; 


     NSString *string1=[[NSString alloc]init]; 
    NSString *string2=[[NSString alloc]init]; 


     /// i wants access these two strings on buttonClicked Method...(pass as a argument):) 

    string1=pro.s_showroomName; 

     string2=pro.s_productName; 


     UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    aButton.frame = CGRectMake(150, 15,150,40); 

    [aButton setTag:indexPath.row]; 

     [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

     [cell.contentView addSubview:aButton]; 

    UIImage *img = [UIImage imageNamed:@"image.png"]; 

     [aButton setImage:img forState:UIControlStateNormal]; 

     [aButton setTitle:@"View Details" forState:UIControlStateNormal]; 

    [aButton addSubview:buttonLabel1]; 


    NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.s_image ofType:@"png"]; 

     UIImage *image=[UIImage imageWithContentsOfFile:filepath]; 



    cell.imageView.image=image; 



    return cell; 
} 

-(void)buttonClicked:(UIButton*)sender { 

    //int tag = sender.tag; 


} 
+0

你是什麼意思傳遞兩個參數?你希望你的單元格包含的信息可以在按鈕回調中訪問嗎? – jbat100

+0

@jbat ...謝謝你給我寶貴的時間........是的..我想要訪問信息包含的單元格...在我的方法buttonClicked ... – GauravBoss

回答

0

一個簡單的方法從按鈕來訪問你的細胞,在按鈕的回調是要求它爲它的上海華盈的上海華:

-(void)buttonClicked:(UIButton*)sender { 

    //int tag = sender.tag; 
    UIView* contentView = [sender superview]; 
    if ([[contentView superview] isKindOfClass:[UITableViewCell class]] == NO) { 
     NSLog(@"Unexpected view hierarchy"); 
     return; 
    } 
    UITableViewCell* cell = (UITableViewCell*)[contentView superview]; 

} 

您還可以根據按鍵的框架上,找出它是哪個細胞渴望在桌子上看。您需要將按鈕的框架轉換爲表格視圖(UIView有方法進行轉換)。

默認的UITableViewCell對象有一個爲textLabel和detailTextLabel,在你的cellForRowAtIndexPath方法,你可以設置自己的文字是這樣的:

cell.textLabel.text = @"Some text"; 
cell.detailTextLabel.text = @"Some detail text"; 

那麼你就可以訪問你的回調,這些屬性,當你已經獲得的細胞。

+0

@ jbat100 .....謝謝再兄弟.......我正在嘗試這..... :) – GauravBoss

+0

...我想在我的表單元格中添加兩個標籤...(showroomName和ProductName)並訪問showroomName.text和ProductName.text在我的ButtonClicked方法......你能給我一點代碼....(在此先感謝) – GauravBoss

+0

添加了一些默認標籤的代碼,並且還檢查了superview的superview確實是一個UITableViewCell,以避免討厭驚喜。 – jbat100