UITableView - when scrolling too fast, the contents of the last cell replace the first cell

This is probably due to poor design, but when I scroll the table too quickly and then scroll it back, the view placed in the last cell of the table also fits on top of the first cell of the table in the View table.

I think this is probably due to my use of if statements to put some static content in the first section and dynamic content in the second section.

Any help is appreciated.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... if (indexPath.section == 0) { if (indexPath.row == 0) { cell.selectionStyle = UITableViewCellSelectionStyleNone; UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 13, 282, 20)]; textField.clearsOnBeginEditing = NO; textField.placeholder = @"enter template name"; textField.font = [UIFont systemFontOfSize:15.0]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.delegate = self; textField.text = [selectedTemplate name]; [textField addTarget:self action:@selector(nameDidChange:) forControlEvents:UIControlEventEditingChanged]; [cell.contentView addSubview:textField]; } else { cell.textLabel.text = @"Add a new question"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } } else { NSString *label = [[sortedQuestions valueForKey:@"questionText"] objectAtIndex:indexPath.row]; CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:NSLineBreakByWordWrapping]; UITextView *textV=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 230, stringSize.height+10)]; textV.font = [UIFont systemFontOfSize:15.0]; textV.text=label; textV.textColor=[UIColor blackColor]; textV.editable = NO; textV.userInteractionEnabled = NO; textV.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f]; [cell.contentView addSubview:textV]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } return cell; } 
+4
source share
3 answers

It seems that the cell that was laid out, for example, for section 0, line 0 is played out and used, for example, for line 0 of line 1, which does not replace all the settings that were made for it when it was holding section 0 of line 0.

You might want to use 3 separate cell identifiers, one for section 0, line 0, one for section 0, line 1, and one for all the others. You will need a preliminary set of if statements (or case-switch statements) so that you use the correct identifier when calling dequeueReusableCellWithIdentifier:

+1
source

Here's what I ended up doing based on the Wienke recommendation. I added 3 cell prototypes to my storyboard called Cell, Cell2, Cell3.

Relevant Code:

 static NSString *CellIdentifier = @"Cell"; static NSString *CellIdentifier2 = @"Cell2"; static NSString *CellIdentifier3 = @"Cell3"; UITableViewCell *cell; if (indexPath.section == 0 && indexPath.row == 0) { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; } else if (indexPath.section == 0 && indexPath.row == 1) { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath]; } else if (indexPath.section == 1) { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3 forIndexPath:indexPath]; } 

I also added this to check my dynamic cells and remove any lingering subqueries that could be hung before adding a new view.

  if ([cell.contentView subviews]){ for (UIView *subview in [cell.contentView subviews]) { [subview removeFromSuperview]; } } 
+4
source

I know that this already has an accepted answer, but I decided that I would go a little deeper into the β€œwhy” this happened and the solution.

In layman's terms, updating the UITableView delayed at a given view speed. In other words, due to fast scrolling, it is expected that the table view will re-populate the content faster than it can sort the expected cell for reuse for the corresponding index (row).

The simplest solution that Wienke has touched is to create different cell identifiers for the first, let them say three (3). This will allow us to see in the tabular presentation a clear differentiation between 3 cells, preventing any displacement of cells.

Perhaps the best approach here would be to assign the appropriate cell IDs to each cell (depending on the context and number of cells). Thus, the table view knows exactly which cell (with its corresponding identifier) ​​goes there. Something simple as shown below:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellID = [NSString stringWithFormat:@"cell%lu", indexPath.row]; __kindof UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { // Create your cell here. cell = [...allocate a new cell...] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } return cell; } 
+1
source

Source: https://habr.com/ru/post/1443567/


All Articles