滚动时如何解决表格视图单元格标题文本重叠的问题 [英] How to fix a Table view cell title text overlap each when it is scrolled

查看:58
本文介绍了滚动时如何解决表格视图单元格标题文本重叠的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,当它滚动时,单元格标题文本彼此重叠.

I have a table view and when it scrolls the cell title text overlap each other.

我在设置cellForIndexPath:错误吗?

I'm I setting up the cellForIndexPath: incorrectly?

这是我的cellForRowAtIndexPath:

here's my 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 不匹配/code>方法.

Your cell height was not matching with the subviews contentsize you are adding in cellForRowAtIndexPath method.

此外,您还应该为每行使用唯一的 Identifier .否则,将重复使用已经创建的具有相同 Identifier "landscape-cell"的单元格,并且不会再次创建.使用

Also you should use unique Identifier for each row. Otherwise the already created cell with the same Identifier "landscape-cell" was re-used and not created again . Use the Identifier something like

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

希望您达到目标.

这篇关于滚动时如何解决表格视图单元格标题文本重叠的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆