2

我有一个UITableView,其中每个UITableViewCell都有一个UIView,在上面UIView我有两个UIButton和两个UILabel。我的问题是,当我重新加载表格视图时,新数据被重写在使内容模糊不清的旧数据上,并且在方法中cellForRowAtIndexPath我使用了删除标签的代码,然后它也无法正常工作......

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UILabel *subview in subviews) {
        [subview removeFromSuperview];
    }
    [subviews release];

此代码一次显示一行,否则没有人...

的代码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+250;
        [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+250;
        [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+250;
        [imgView addSubview:button];

        button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(5, 7, 19, 21);
        button.tag = indexPath.row+250;
        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+250;
        [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];

        NSDictionary *dict1 =[appDelegate.webDataList objectAtIndex:indexPath.row];


        label = [[UILabel alloc] initWithFrame:CGRectMake(181, 7, 75, 20)];
        label.tag = indexPath.row+250;
        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;
}

在我的代码中,我无法使用该方法reuseTableViewCellWithIdentifier:,因为我希望每个标签和按钮标签都应该相同,因为我必须在其他地方获取它们。

4

1 回答 1

3

发生这种情况是因为您做错了。您甚至不需要重新加载表格。只需滚动一下,您就会注意到它。
每次显示单元格时,您都会创建新的标签和按钮。当然,这些 UI 元素永远不会被删除。

在创建新单元格时添加 UI 元素并给它们一个标签。如果单元格成功出列,则通过该标记获取元素并设置其值。

像这样:

if (cell == nil) {
    // if no cell could be dequeued create a new one
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

    // add label to the new cell
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)];
    label.tag = 250;  // <---- save tag
    [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]; // I ignore this because I don't want to write 200 lines of code here. This is just an example so please adopt the code yourself
    [cell.contentView addSubView:label];
    [label release];
}

// whatever happened you have a cell with all the labels added here

UILabel *label = (UILabel *)[cell.contentView viewWithTag:250]      // <---- get label with tag
label.text = [arr objectAtIndex:1];

你不需要if([appDelegate.dataArray count]>0). tableView:cellForRowAtIndexPath:如果您的数据源为空,则不会调用所有其他 tableview 数据源方法是否正确。


编辑:刚看到那个:

在我的代码中,我无法使用方法 reuseTableViewCellWithIdentifier: 因为我希望每个标签和按钮标签应该相同,因为我必须在其他地方获取它们。

其实你可以。每个人都可以而且应该使用reuseTableViewCellWithIdentifier:.
你的具体问题是什么?
如果您需要在按钮操作方法中获取 indexPath (或者在您的情况下只有行),请使用我对另一个问题的回答中的代码

于 2011-12-15T11:10:18.717 回答