Iphone UITableView重载数据方法会造成数据模糊的问题

Iphone UITableView重载数据方法会造成数据模糊的问题,iphone,objective-c,ios,uitableview,reloaddata,Iphone,Objective C,Ios,Uitableview,Reloaddata,我有一个UITableView,其中每个UITableView单元格都有一个UIView,并且在UIView上方有两个UIButton和两个UILabel。我的问题是,当我重新加载tableview时,新数据被重写在旧数据上,使内容模糊和不清楚,在方法cellforrowatinexpath中,我使用了删除标签的代码,但它也不能正常工作 NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];

我有一个
UITableView
,其中每个
UITableView单元格都有一个
UIView
,并且在
UIView
上方有两个
UIButton
和两个
UILabel
。我的问题是,当我重新加载tableview时,新数据被重写在旧数据上,使内容模糊和不清楚,在方法
cellforrowatinexpath
中,我使用了删除标签的代码,但它也不能正常工作

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UILabel *subview in subviews) {
        [subview removeFromSuperview];
    }
    [subviews release];
此代码一次显示一行,否则没有人

cellforrowatinexpath
的代码是

- (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:
方法,因为我希望每个标签和按钮标记都应该相同,因为我必须在其他地方提取它们。

发生这种情况是因为你做得不对。您甚至不需要重新加载表。只要滚动一下,你就会注意到它。
每次显示单元格时,都可以创建新标签和按钮。当然,这些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(单元格==nil){
//如果没有单元格可以退出队列,请创建一个新的单元格
单元格=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:标识符]自动释放];
//将标签添加到新单元格
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(34,9140,20)];
label.tag=250;//0)
。如果所有其他tableview数据源方法都正确,则不会在数据源为空时调用
tableview:CellForRowatineXpath:


编辑:刚刚看到了那个:

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

实际上你可以。每个人都可以而且应该使用
reuseTableViewCellWithIdentifier:

你到底有什么问题?
如果需要在按钮操作方法中获取indexPath(或者在您的情况下仅获取行),请使用我的