2011-01-25 67 views
9

我有这个奇怪的问题,我的桌子重叠的UITableViewCell - 与以往的细胞内容

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

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

这里是我的代码,将增加一些照片太 Cell with overlapped content

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


} 
+0

你应该在nib中构建像这样的单元格,而不是在cellForRow中创建所有的东西。 – Mark 2013-04-23 22:07:09

+0

你也可以在这里看到答案。清楚地解释。 http://stackoverflow.com/questions/8999300/labels-in-custom-cell-overlaps – user3675974 2014-07-09 07:36:17

+0

我有同样的问题,但我的子视图没有在代码中实例化,但来自故事板。如果删除子视图,他们都会消失。我该如何解决这个问题? – DamirDiz 2015-07-27 16:29:39

回答

19

我想通了一些试验和错误;如果这能帮助一些一

在的cellForRowAtIndexPath我试图清除绘制细胞

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

我会很乐意之前所有的细胞子视图如果有人能指出我一些更简单的方法

感谢

7

您可以使用

​​

如果不是零的单元格,上面的代码将减少时间使用类似这种

for(UIView *eachView in [cell subviews]) 
    [eachView removeFromSuperview]; 
1

我知道这是有点晚循环,但我在那里UILabels为创建一个类似的问题细胞在重复使用时仍然是细胞的一部分。因此,tableview的每个连续更新都在现有的更新之上创建了另一个UILabel。我将标签的创建移动到if条件如下,它解决了我的问题。希望它可以帮助别人。另请注意,因为我正在使用ARC,因此无法发布。

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 

    { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    cityText = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; 
    cityText.font = [UIFont fontWithName:@"Arial" size:20]; 
    cityText.textAlignment = UITextAlignmentLeft; 
    cityText.backgroundColor = [UIColor clearColor]; 

    regionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 20)]; 
    regionText.font = [UIFont fontWithName:@"Arial" size:20]; 
    regionText.textAlignment = UITextAlignmentLeft; 
    regionText.backgroundColor = [UIColor clearColor]; 

    } 
5

我也有同样的issue.I还组成了以的tableView当labels.And我滚动下来的内容得到了overlapped.But它得到了由cellForRowAtIndexPath编辑一个语句简单解决。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

我认为这会解决您的问题。

0

设置上的UITableViewCell子类的方法prepareForReuse标签文本()解决我的问题 -

override func prepareForReuse() { 
    super.prepareForReuse() 
    self.label1.text = nil 
    self.label2.text = nil 
    .... 
} 

共享,如果它可以帮助任何人!