2011-12-14 46 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; 
相關問題