2011-12-14 88 views
1

我正在创建一个应用程序,其中有一个UITableView和表视图每个单元格都有一个UIView,而UIView包含两个自定义UIButton和两个UILabel标记)来自XML解析的标签的值。现在我必须做的是,当我点击任何一个按钮,然后它调用按钮的标签的方法,然后我想获取该单元格的两个标签的值,并使用该值。无法通过自定义方法从UITableViewCell获取值

但是,当我这样做这不是通过调用一个方法我得到的标签值,但是当我尝试获得标签值,然后我无法得到。

有没有什么办法来得到它的价值?

代码,我使用,以获取标签值的方法...

-(void)swapButtonPressed:(id)sender { 
    NSInteger a= [sender tag]; 

    UITableViewCell *cell; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *) [[sender superview] superview]]; 
    NSLog(@"%d", indexPath.row); 

    UIView *viewForCell = (UIView *)[cell.contentView viewWithTag:a]; 

    UILabel *label = (UILabel *)[viewForCell viewWithTag:a]; 
    NSString *str = label.text; 
} 

方法的cellForRowAtIndexPath的代码....

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
    } 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    NSDictionary *dict = [appDelegate.webDataList objectAtIndex:indexPath.row]; 

    tableView.backgroundColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:0.39]; 

    if([appDelegate.dataArray count]>0){ 

     imgView.backgroundColor = [UIColor clearColor]; 
     imgView =[[UIView alloc ] initWithFrame:CGRectMake(0,0,320,45)]; 
     imgView.tag= indexPath.row; 
     [cell.contentView addSubview:imgView]; 

     button=[UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame=CGRectMake(291, 5, 19, 21); 
     img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     button.tag= indexPath.row; 
     [imgView addSubview:button]; 

     button=[UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame=CGRectMake(-25, 5, 19, 21); 
     img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     button.tag= indexPath.row; 
     [imgView addSubview:button]; 

     button=[UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame=CGRectMake(5, 7, 19, 21); 
     button.tag = indexPath.row; 
     img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"delete" ofType:@"png"]]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];  
     [imgView addSubview:button]; 

     NSArray *arr = [[appDelegate.dataArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"/"]; 


     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)]; 
     label.text = [arr objectAtIndex:1]; 
     label.tag = indexPath.row; 
     [label setFont:[UIFont fontWithName:@"Georgia" size:14]]; 
     [label setNumberOfLines:0]; 
     label.textAlignment = UITextAlignmentLeft; 
     label.textColor = [UIColor whiteColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     label.backgroundColor = [UIColor clearColor]; 
     [imgView addSubview:label]; 
     [label release]; 


     label = [[UILabel alloc] initWithFrame:CGRectMake(181, 7, 75, 20)]; 
     label.tag = indexPath.row; 
     label.text = [dict1 objectForKey:@"time"]; 
     [label setFont:[UIFont fontWithName:@"Georgia" size:16.0]]; 
     label.textAlignment = UITextAlignmentLeft; 
     [label setBackgroundColor:[UIColor clearColor]]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [label setTextColor:[UIColor whiteColor]]; 
     [imgView addSubview:label]; 
     [label release]; 
    } 
    return cell; 
} 
+0

你如何设置你的标签文本,也可以发布'cellForRowAtIndexPath`方法? – 2011-12-14 13:16:09

+0

你可以给cellForIndex ...方法代码吗? – Maulik 2011-12-14 13:18:55

回答

1

我看到两个问题:

  1. 标签必须> 0,所以你不能直接使用indexPath.row

  2. 标签需要一个上海华内是唯一的

作为@Kenny表明,获得从细胞indexPath和直接访问数据源一般是更可靠的。如果您想从单元格中的对象获取值,请使用大于0的唯一标签。我通常在子视图中为标签定义一个枚举,以使其更具可读性。

0

我认为viewWithTag是递归的,所以在你的代码中,这意味着你在第一次调用它的时候返回标签,而在第二次调用时没有。因此,也许这会工作:

UILabel *label = (UILabel *)[cell.contentView viewWithTag:a]; 
NSString *str = label.text; 

不过,我建议你从你的模型对象获得的价值,你可能得到它,当你设置按钮上的字符串中的首位,这样的事情:

NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *) [[sender superview] superview]]; 
TheObect *obj = [myobjects objectAtIndex:indexPath.row]; 
NSString *string = obj.name; 
相关问题