无法通过自定义方法从UITableViewCell获取值

时间:2011-12-14 12:52:32

标签: iphone objective-c ios uitableview

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

但是当我这样做时,通过调用一个方法我没有获得标签值,但当我尝试获取标签值时,我无法得到。

有没有办法获得它的价值?

我用来获取标签值的方法代码...

-(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;
}

2 个答案:

答案 0 :(得分:1)

我看到两个问题:

  1. 标签必须是> 0因此您无法直接使用indexPath.row

  2. 标签在superview中必须是唯一的

  3. 正如@Kenny建议的那样,从单元格中获取indexPath并直接访问数据源通常更可靠。如果您想从单元格中的对象获取值,请使用唯一标记>我通常在子视图中为标签定义枚举,以使事物更具可读性。

答案 1 :(得分:0)

我认为viewWithTag是递归的,所以在你的代码中,这意味着你在第一次调用时返回标签,在第二次调用中返回nil。所以也许这会奏效:

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;