2011-10-22 87 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