Merge content of UITableViewCell on scroll

When I open the ViewController, I see a normal UITableView with the correct content as shown below.

before scrolling tableview

and when i tried scrolling up and down then the sudden content merged like below. after scrolling tableview help me solve this problem.

My code

#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

+3


source to share


2 answers


The problem may be due to cell reuse. The best way to work with tableView cells is to customize the cell. Then you don't need to add interface elements to cellForRowAtIndexPath:

.

For an instant solution, in your current code, remove the existing subview in case of reuse,



[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

      

+1


source


When you use reusable cells, and for each weather cell that can be reused or just created, you create a new label and add it according to the kind of cell content.

For a newly created cell, adding a label is not a problem.

But the problem arises when the cell is reused, already contains one label, and another label is created again, which precedes the previous label and is therefore responsible for overlapping one label with another.

To solve this problem, you have two options -

1) update the label content of multiple cells with label data for the current pointer path



2) in case of overwriting the previous cell label and creating a new one, initialize the data for the current row.

Using the second approach, I have updated your implementation of the cell creation handler as -

- (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 = [[cell contentView] viewWithTag:indexPath.row];
    if(label ! = nil)
          [label removeFRomSuperView];

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

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

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

    [label release];

    return cell;
}

      

You can use the above implementation to solve the overlap issue due to multiple label existing in a reusable cell.

+1


source







All Articles