Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell content merging on scrolling

When i open ViewController i can see normal UITableView with correct content as shown in below image

before scrolling tableview

and when i tried to scroll up and down then suddenly contents merged like this below image. after scrolling tableview please help me to solve this issue

My code is

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [[announcement valueForKey:@"title"] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor clearColor];
    UILabel *label = nil;

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"];



        }
    label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setLineBreakMode:UILineBreakModeWordWrap];
    [label setMinimumFontSize:FONT_SIZE];
    [label setNumberOfLines:0];
    [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    [label setTag:1];


    [[cell contentView] addSubview:label];



    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [[[announcement valueForKey:@"title"] objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    if (!label)
        label = (UILabel*)[cell viewWithTag:1];
    label.textColor = [UIColor colorWithRed:93.0/255.0 green:93.0/255.0 blue:93.0/255.0 alpha:1.0];
    label.font = [UIFont fontWithName:kFontHelveticaNeueBold size:cell.textLabel.font.pointSize];



    label.text=[[announcement valueForKey:@"title"] objectAtIndex:indexPath.row];

    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


            NSString *text =[[announcement valueForKey:@"title"] objectAtIndex:indexPath.row];

            CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

            CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

            CGFloat height = MAX(size.height, 44.0f);

           return height + (CELL_CONTENT_MARGIN * 2);

}

please help

like image 392
abhi bangalore Avatar asked Jun 03 '26 12:06

abhi bangalore


1 Answers

Problem may be because of the cell reusing. The best method of working with tableView cells is customise cell. Then you need not add UI elements in cellForRowAtIndexPath:.

For instant solving in your current code, remove the existing subview in the case of reusing,

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
like image 93
Anusha Kottiyal Avatar answered Jun 06 '26 01:06

Anusha Kottiyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!