如何修复表视图单元格标题文本在滚动时重叠

我有一个表格视图,当它滚动时,单元格标题文本相互重叠。

在此处输入图像描述

我是在设置cellForIndexPath:不正确?

这是我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath{ BOOL isLandscape = YES; if (nextOrientation == UIInterfaceOrientationPortrait || nextOrientation == UIInterfaceOrientationPortraitUpsideDown) { isLandscape = NO; } UIImageView* bgImage; UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:(isLandscape) ? @"landscape-cell":@"portrait-cell"]; if (cell == nil){ // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:(isLandscape) ? @"landscape-cell":@"portrait-cell"] autorelease]; cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:(isLandscape) ? @"landscape-cell":@"portrait-cell"] autorelease]; if (isLandscape) { bgImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480, 125)] autorelease]; [bgImage setTag:BACK_IMAGE]; // [cell addSubview:bgImage]; }else { bgImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 125)] autorelease]; [bgImage setTag:BACK_IMAGE]; [cell addSubview:bgImage]; } }else { bgImage = (UIImageView*)[cell viewWithTag:BACK_IMAGE]; } for (UIView* v in cell.subviews) { if ([v isKindOfClass:[CatalogeView class]]) { [v removeFromSuperview]; } } if (isLandscape) { if (IPADAPP) { [bgImage setFrame:CGRectMake(0, 0, 1024, 250)]; [bgImage setImage:[ImgUtil image:@"polka_gor@2x.png"]]; int num = 0; for(int i = indexPath.row*CATALOGE_ON_SHELF_HORIZONTAL; i < [cataloges count] && i < (indexPath.row + 1)*CATALOGE_ON_SHELF_HORIZONTAL; i++){ CatalogeView* catalogeView = [cataloges objectAtIndex:i]; [catalogeView setPosition:CGPointMake(40 + 120*(num%CATALOGE_ON_SHELF_HORIZONTAL) * 2 , 30)]; [cell addSubview:catalogeView]; num++; } }else{ [bgImage setFrame:CGRectMake(0, 0, 480, 125)]; [bgImage setImage:[ImgUtil image:@"polka_gor.png"]]; int num = 0; for(int i = indexPath.row*CATALOGE_ON_SHELF_HORIZONTAL; i < [cataloges count] && i < (indexPath.row + 1)*CATALOGE_ON_SHELF_HORIZONTAL; i++){ CatalogeView* catalogeView = [cataloges objectAtIndex:i]; [catalogeView setPosition:CGPointMake(20 + 120*(num%CATALOGE_ON_SHELF_HORIZONTAL),15)]; [cell addSubview:catalogeView]; num++; } } }else { if (IPADAPP) { [bgImage setFrame:CGRectMake(0, 0, 768, 250)]; [bgImage setImage:[ImgUtil image:@"polka@2x.png"]]; int num = 0; for(int i = indexPath.row*CATALOGE_ON_SHELF_VERTICAL; i < [cataloges count] && i < (indexPath.row + 1)*CATALOGE_ON_SHELF_VERTICAL; i++){ CatalogeView* catalogeView = [cataloges objectAtIndex:i]; [catalogeView setPosition:CGPointMake(20 + 105*(num%CATALOGE_ON_SHELF_VERTICAL) * 2.5 ,30)]; [cell addSubview:catalogeView]; num++; } }else{ [bgImage setFrame:CGRectMake(0, 0, 320, 125)]; [bgImage setImage:[ImgUtil image:@"polka.png"]]; int num = 0; for(int i = indexPath.row*CATALOGE_ON_SHELF_VERTICAL; i < [cataloges count] && i < (indexPath.row + 1)*CATALOGE_ON_SHELF_VERTICAL; i++){ CatalogeView* catalogeView = [cataloges objectAtIndex:i]; [catalogeView setPosition:CGPointMake(10 + 105*(num%CATALOGE_ON_SHELF_VERTICAL),15)]; [cell addSubview:catalogeView]; num++; } } } return cell; } 

和目录视图类:

  @implementation CatalogeView @synthesize delegate; @synthesize cataloge; - (void)dealloc { [cataloge release]; [image release]; [title release]; [button release]; [super dealloc]; } +(id) make{ NSInteger koef = 1; if (IPADAPP) { koef = 2; } CatalogeView* ctrl = [[[CatalogeView alloc] initWithFrame:CGRectMake(0, 0, FULL_VIEW_WIDTH * koef, FULL_VIEW_HEIGTH * koef)] autorelease]; [ctrl addAllSubviews]; return ctrl; } -(void) addAllSubviews{ NSInteger koef = 1; if (IPADAPP) { koef = 2; } if (!title) { title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, FULL_VIEW_WIDTH * koef, TITLE_HEIGTH * koef)]; [title setNumberOfLines:2]; [title setTextColor:[UIColor whiteColor]]; [title setFont:[UIFont fontWithName:@"Helvetica" size:9 * koef]]; [title setTextAlignment:UITextAlignmentCenter]; [title setBackgroundColor:[UIColor clearColor]]; [self addSubview:title]; } if (!image) { image = [[UIImageView alloc] initWithFrame:CGRectMake(0, TITLE_HEIGTH * koef, FULL_VIEW_WIDTH * koef, IMAGE_HEIGTH * koef)]; [image setBackgroundColor:[UIColor clearColor]]; [self addSubview:image ]; } if (!button) { button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, FULL_VIEW_WIDTH * koef, FULL_VIEW_HEIGTH * koef)]; [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button ]; } } -(void) addCatalogeInView:(CatalogeDbo*) newCataloge{ self.cataloge = newCataloge; [title setText:cataloge.realName]; [image setImage:[DownloadImage getImageWithName:[NSString stringWithFormat:@"%@.png", cataloge.image]]]; } -(void) setPosition:(CGPoint) position{ NSInteger koef = 1; if (IPADAPP) { koef = 2; } [self setFrame:CGRectMake(position.x, position.y, FULL_VIEW_WIDTH * koef, FULL_VIEW_HEIGTH * koef)]; } -(void) click{ if(cataloge){ [delegate clGoInCataloge:cataloge]; } } @end 

您的单元格高度与您在cellForRowAtIndexPath方法中添加的subviews contentsize不匹配。

您还应该为每一行使用唯一Identifier 。 否则,已重新使用具有相同Identifier “landscape-cell”的已创建单元格,而不是再次创建。 使用Identifier东西

 [NSString stringWithformat: "cell_%d",indexpath.row]; 

希望你达到目标。

阅读所有这些,我的假设是你多次添加CatalogeView ,这会导致每次单元格出列时标签,按钮和图像重叠(最后两个你看不到重叠)。 相反,创建一个更改CatalogeView对象值的函数,并在已添加它们时设置它们。

每次生成单元格时,您都会动态创建新的ui元素。 为什么不在新的nib文件中创建自定义单元格(或两个,横向和纵向)。 通过这种方式,您可以访问所需的元素,并且标签不会重叠,因为您可以在InterfaceBuilder中将它们设置为所需的位置。 而你只需要称他们为:

 [cell.label1 setText [cell.label2 setText.... 

请记住,向UITableViewCells添加额外的SubViews可能会导致问题。 你应该保持子视图的数量尽可能低,这样UITableView就不会变慢。 在WWDC流中有一个很好的解释: iOS应用程序性能:图形和动画

您可能错误地计算了您的帧。 用于横向或纵向模式

如果您使用的是custum单元格,则必须为行定义高度,如果使用单元格的addsubview,则必须提供所有子视图的正确位置。

似乎heightForRowAtIndexPath(或xib设置)返回的不同高度比您的CatalogueView导致这样的重叠。 另外,为什么不使用UICollectionView(或PSTCollectionView以防你需要支持iOS 5.x)?

! ВовкавПростоквашиноиДюймовочкаблудногопопугая – можноновыемультфильмыснимать。 :)(有些人谈论由重叠的名字引起的俄罗斯漫画)

如果你仍然面临问题,写下面

 cell=nil; // use this to create new cell if (cell == nil){ cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: }