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

时间:2013-10-02 12:13:20

标签: ios objective-c uitableview

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

enter image description here

我是在设置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

7 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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

此外,您应该为每一行使用唯一的Identifier。否则,已重新使用已创建的具有相同Identifier“landscape-cell”的单元格,而不会再次创建。 使用Identifier之类的内容

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

希望你达到目标。

答案 2 :(得分:1)

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

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

请注意,向SubViews添加额外的UITableViewCells可能会导致问题。您应该保持子视图的数量尽可能低,以便UITableView不会变慢。在WWDC流中有一个很好的解释: iOS App Performance: Graphics and Animations

答案 3 :(得分:1)

你可能错误地计算了你的帧数。对于横向或纵向模式

答案 4 :(得分:0)

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

答案 5 :(得分:0)

如果您仍然遇到问题,请在下面写下

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

答案 6 :(得分:0)

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

  

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