UITableViewCell - 与先前的单元格内容重叠

Vee*_*eru 10 cocoa-touch uitableview

我的桌子有这个奇怪的问题

  1. 我有大约20个细胞展示
  2. 每个单元的高度约为84px
  3. 当我没有单击单元格时,我设置了背景颜色
  4. 前4个单元格没问题,但是当我向下滚动并点击第5个单元格时,每个单元格的内容开始与其他一些内容重叠,通常是来自前4个单元格的内容.

我相信它的一些细胞可重用性或绘图问题.我不确定如何解决它,我已经检查了我的代码,但我没有改变单元的触摸内容.

这是我的代码,也将添加一些图片 具有重叠内容的单元格

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 104;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     return [stores count];
}

-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    CGRect Label1Frame = CGRectMake(5, 5, 250, 30);
    CGRect Label2Frame = CGRectMake(6, 42, 220, 20);    
    CGRect Label3Frame = CGRectMake(6, 62, 220, 20);        
    CGRect Label4Frame = CGRectMake(240,56, 70, 12);            

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }else{
        // a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields)
        UIView *viewToCheck = nil;
        viewToCheck = [cell.contentView viewWithTag:1];
        if (!viewToCheck) {
            [viewToCheck removeFromSuperview];
            DebugLog(@"View removed");
        }       
    }
    //cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell setSelectedBackgroundView:bgView];    
    NSInteger row=indexPath.row;
    UILabel *lblTemp;
    [[cell contentView] clearsContextBeforeDrawing];

    //Line 1

    lblTemp=[[UILabel alloc] initWithFrame: Label1Frame];
    lblTemp.tag=1;
    lblTemp.text=[[stores objectAtIndex:row] objectAtIndex:0] ;
    lblTemp.numberOfLines=2;
    lblTemp.font = [UIFont boldSystemFontOfSize:13];
    lblTemp.adjustsFontSizeToFitWidth=YES;
    lblTemp.minimumFontSize=12;
    lblTemp.textColor = [UIColor grayColor];
    [cell.contentView addSubview:lblTemp];  
    [lblTemp release];
    //Line 2
    lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
    lblTemp.tag = 2;
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:1];   
    lblTemp.font = [UIFont systemFontOfSize:12];
    lblTemp.textColor = [UIColor grayColor ];
    lblTemp.textAlignment=UITextAlignmentLeft;
    lblTemp.adjustsFontSizeToFitWidth=YES;
    lblTemp.minimumFontSize=12;

    [cell.contentView addSubview:lblTemp];  
    [lblTemp release];

    //Line 3    
    lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
    lblTemp.tag = 3;
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:2];
    lblTemp.font = [UIFont systemFontOfSize:12];
    lblTemp.textColor = [UIColor grayColor ];
    [cell.contentView addSubview:lblTemp];      
    [lblTemp release];
    //Phone button
    UIButton *phoneButton=[[UIButton alloc] initWithFrame:CGRectMake(240,16,30,30)];
    [phoneButton setBackgroundImage:[UIImage imageNamed:@"phone.png"] forState:UIControlStateNormal];
    [phoneButton setTag:row];
    [phoneButton addTarget:self action:@selector(dialNumber:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:phoneButton];
    //ANnotation button
    UIButton *annotation=[[UIButton alloc] initWithFrame:CGRectMake(274,16,30,30)]; 
    [annotation setTag:row];
    [annotation setBackgroundImage:[UIImage imageNamed:@"tab.png"] forState:UIControlStateNormal];
    [annotation addTarget:self action:@selector(openMap:) forControlEvents:UIControlEventTouchUpInside];    
    [cell.contentView addSubview:annotation];   
    [annotation release];
    //Distance label
    //Line 3    
    lblTemp = [[UILabel alloc] initWithFrame:Label4Frame];
    lblTemp.tag = 4;
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:5];   
    lblTemp.textAlignment=UITextAlignmentCenter;
    lblTemp.font = [UIFont systemFontOfSize:13];
    lblTemp.textColor = [UIColor grayColor ];
    [lblTemp setAdjustsFontSizeToFitWidth:YES];
    [cell.contentView addSubview:lblTemp];      
    [phoneButton release];
    [lblTemp release];  
    [cell setNeedsLayout];
    [cell setNeedsDisplay];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath ];   
    for(UILabel *lbl in cell.contentView.subviews){
        if([lbl isKindOfClass:[UILabel class]]){
            lbl.textColor=[UIColor whiteColor];

        }
    }
    //UITableViewCell *cell1;       
    //NSString *row=[NSString stringWithFormat:@"%d",indexPath.row];
    svm = [[storesMapView alloc] initWithNibName:@"storesMapView" bundle:nil];
    [svm initWithXML:stores:indexPath.row];
    CGRect theFrame = svm.view.frame;
    theFrame.origin = CGPointMake(self.view.frame.size.width, 0);
    svm.view.frame = theFrame;
    theFrame.origin = CGPointMake(0,0);
    theFrame.size=CGSizeMake(320,355);
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3f];
    svm.view.frame = theFrame;
    [UIView commitAnimations];
    [subView addSubview:svm.view];
    backButton.hidden=NO;


}
Run Code Online (Sandbox Code Playgroud)

Vee*_*eru 19

我通过一些反复试验弄清楚了; 如果这可以帮助一些人

在cellforRowAtIndexPath中,我试图在绘制单元格之前清除所有单元格子视图

//TRY TO REMOVE ALL CONTENT
    for(UIView *view in cell.contentView.subviews){
        if ([view isKindOfClass:[UIView class]]) {
            [view removeFromSuperview];
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果有人能指点我一些更简单的方法,我会很高兴

谢谢


小智 7

您可以使用

[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
Run Code Online (Sandbox Code Playgroud)

如果单元格不是nil,则上面的代码将减少使用类似于此的for循环的时间

for(UIView *eachView in [cell subviews])
    [eachView removeFromSuperview];
Run Code Online (Sandbox Code Playgroud)


iOS*_*iOS 5

我也有同样的问题.我也用tables组合了tableView.当我向下滚动时,内容重叠了.但它只是通过编辑一个语句来解决cellForRowAtIndexPath.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
Run Code Online (Sandbox Code Playgroud)

我想这会解决你的问题.