Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicated text in UITableView

I got an iOS app with table view. When i select rows several times, information in selected one duplicate.

You can see it on a picture: enter image description here

I add cell's in this way:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    UILabel *label = [[UILabel alloc] init];
    Place *item = [source objectAtIndex:indexPath.row];
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]];
    label.text =item.name;
    label.frame = CGRectMake(5, 5, 310, 35);
    [cell addSubview:label];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    OnePlaceViewController *placeView = [[OnePlaceViewController alloc] initWithNibName:@"OnePlaceViewController" bundle:nil];
    placeView.nom = indexPath.row;
    placeView.source = source;
    placeView.route = route;
    placeView.titleView = titleView;
    /*
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    */
    [self.navigationController pushViewController:placeView animated:YES];
}

Why do information duplicate? Thnx.

like image 771
Eugene Trapeznikov Avatar asked Mar 26 '12 12:03

Eugene Trapeznikov


People also ask

What is IndexPath UITableView?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.

What is the purpose of the reuseIdentifier?

The reuse identifier is associated with a UITableViewCell object that the table-view's delegate creates with the intent to reuse it as the basis (for performance reasons) for multiple rows of a table view. It is assigned to the cell object in init(frame:reuseIdentifier:) and cannot be changed thereafter.

What is dequeueReusableCell?

dequeueReusableCell(withIdentifier:)Returns a reusable table-view cell object after locating it by its identifier.

What is UITableView?

UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.


2 Answers

you're adding a label EVERYTIME.. change it like that:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    // Configure the cell...
    UILabel *label = [[UILabel alloc] init];
    Place *item = [source objectAtIndex:indexPath.row];
    [label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]];
    label.frame = CGRectMake(5, 5, 310, 35);
    label.tag = 666;
    [cell addSubview:label];
    // add this unless you're using ARC
    // [label release];
    // [cell autorelease];
  }

  UILabel* cellLabel = (UILabel*)[cell viewWithTag: 666];
  cellLabel.text = item.name;

  return cell;
}
like image 136
calimarkus Avatar answered Oct 09 '22 03:10

calimarkus


I think the problem is that you're adding UILabel as subView everytime. Since cells are reused, everytime the table reloads, multiple UILabels are added. So, my suggestion would be to go through all the subviews of the cell and remove them before adding new label.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
UILabel *label = [[UILabel alloc] init];
Place *item = [source objectAtIndex:indexPath.row];
[label setFont:[UIFont fontWithName:@"PermianSansTypeface" size:15.0]];
label.text =item.name;
label.frame = CGRectMake(5, 5, 310, 35);
for(UIView *v in [cell subviews])
{
    if([v isKindOfClass:[UILabel class]])
       [v removeFromSuperview];
}
[cell addSubview:label];
return cell;
}
like image 20
tipycalFlow Avatar answered Oct 09 '22 01:10

tipycalFlow