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:
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.
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.
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.
dequeueReusableCell(withIdentifier:)Returns a reusable table-view cell object after locating it by its identifier.
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.
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;
}
I think the problem is that you're adding UILabel
as subView everytime. Since cells are reused, everytime the table reloads, multiple UILabel
s 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With