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

标签 iphone objective-c ios uitableview

我正在创建一个应用程序,其中我有一个 UITableView 和 TableView ,每个单元格都有一个 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;
}

最佳答案

我看到两个问题:

  1. 标签需要大于 0,因此您不能直接使用 indexPath.row

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

正如@Kenny 所建议的,从单元格获取indexPath 并直接访问数据源通常更可靠。如果您想从单元格中的对象获取值,请使用 > 0 的唯一标签。我通常为 subview 中的标签定义一个枚举以使内容更具可读性。

关于iphone - 无法通过自定义方法从 UITableViewCell 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8504802/

相关文章:

iphone - 将整数转换为日期字符串

objective-c - 我可以将 Objective-C 方法标记为仅可用于 Swift 类吗?

iOS 应用内购买未显示在 App Store 构建中

ios - 当模态呈现另一个 View Controller 时,如何完全关闭 View Controller 的所有后台工作

iphone - 如何将这些左箭头和右箭头添加到我的工具栏?

iphone - 如何获取自定义tableViewCell中UIButton的indexPath?

iphone - 从我的图库添加照片时出错?

ios - 将 Objective-C 框架转换为 Swift - 头文件仍然显示 Objective-C 函数

ios - 获取 View Controller 当前比例

iPhone - 将按钮添加到键盘现有附件 View (来自 UIWebView 的键盘)