How to fix a Table view cell title text overlap ea

2020-06-30 03:50发布

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

enter image description here

I'm I setting up the cellForIndexPath: incorrectly?

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

and the Catalogue View class:

    @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条回答
干净又极端
2楼-- · 2020-06-30 04:16

write below if you still face problem

cell=nil;  // use this to create new cell
if (cell == nil){
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:
}
查看更多
ゆ 、 Hurt°
3楼-- · 2020-06-30 04:17

You might be calculating incorrectly your frames. For Landscape or portrait mode

查看更多
倾城 Initia
4楼-- · 2020-06-30 04:19

if You are using a custum cell then you have to define height for row,and if you are using addsubview of cell then you have to provide correct positions of all subviews.

查看更多
欢心
5楼-- · 2020-06-30 04:22

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

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

Hope you achieve the target.

查看更多
女痞
6楼-- · 2020-06-30 04:34

Reading all this, my assumption is that you're adding CatalogeView multiple times and this causes the labels, buttons and images to overlap (the last two you just don't see overlapping) everytime a cell is dequeued. Instead, make a function that changes the values for the objects in CatalogeView and set them if it's already been added.

查看更多
Emotional °昔
7楼-- · 2020-06-30 04:41

You're creating new ui elements dynamically every time that the cell's will be generated. Why don't you just create a custom cell (or two, landscape and portrait) inside a new nib file. This way you can access the elements you want and the labels won't overlap because you just can set them to the position you want, inside the InterfaceBuilder. And you just would need to call them like :

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

And keep in mind, adding additional SubViews to UITableViewCells might cause a problem. You should keep the number of subviews as low as possible so the UITableView doesn't get to slow. There is a good explanation in a WWDC Stream: iOS App Performance: Graphics and Animations

查看更多
登录 后发表回答