Search code examples
iostagsuitableviewcell

iOS: UITableView cell.tag number keeps increasing when in and out of the View


How can I keep track if the Cell was assigned a tag number? I'll have anywhere from 10 - 90 cells created. However, when I scroll up and down (in and out-of-view), after cells are recreated, the cell Tag number continue to increase.

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

    NSManagedObject *managedObject = rowModels[indexPath.row];
    NSString  *entityName= [[managedObject entity]name];
    cell.textLabel.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];


    cell.tag = cellTagIndex++;
    NSString *tagString = [NSString stringWithFormat:@"%d", cell.tag];

    //cellToObjectMap is NSMutableDictionary

    if ([entityName isEqualToString:@"Test1" ])
    {
        [cellToObjectMap setValue:[managedObject valueForKey:@"test1_id"] forKey:tagString];
    }
    else if ([entityName isEqualToString:@"Test2t" ])
    {
        [cellToObjectMap setValue:[managedObject valueForKey:@"test2_id"] forKey:tagString];
    }

    NSLog(@"Cell.tag %@    Value:  %@", tagString, [cellToObjectMap objectForKey:tagString]);

    return cell;

}

Log:

2013-08-15 19:07:57.243 Time[8510:c07] Cell.tag 0    Value:  95AB73F8-E0C2-4D17-B6BE-9FC2E696959D
2013-08-15 19:07:57.244 Time[8510:c07] Cell.tag 1    Value:  EA163C15-0CF1-4275-B939-0ED9F62C240D
2013-08-15 19:07:57.245 Time[8510:c07] Cell.tag 2    Value:  DDFD36F6-4CAD-4C26-A38C-B22D827199BD
2013-08-15 19:07:57.245 Time[8510:c07] Cell.tag 3    Value:  AC9FD255-08CF-44C4-B168-E6441D08AC54
2013-08-15 19:07:57.246 Time[8510:c07] Cell.tag 4    Value:  48C2DE0C-88C3-48CC-BBB1-350088EE9862
2013-08-15 19:07:57.247 Time[8510:c07] Cell.tag 5    Value:  5C4495A1-FF3D-439E-BD01-88312EF881AD

........  
........ // Cell.tag continues to increase as I scroll up and down the screen.   

2013-08-15 19:10:38.903 Time[8510:c07] Cell.tag 254    Value:  81CAA228-51F4-464C-8B2B-12D2A66C3BA8
2013-08-15 19:10:38.969 Time[8510:c07] Cell.tag 255    Value:  7DC03657-B987-4994-8066-5393F77D891B
2013-08-15 19:10:39.052 Time[8510:c07] Cell.tag 256    Value:  BB1BCFD2-FBD9-46FA-B662-56085254FD46
2013-08-15 19:10:39.169 Time[8510:c07] Cell.tag 257    Value:  0C78EBE0-AD83-4A21-8546-37EA17EAB5CA

How can I keep track if a cell number was already assigned a cell.tag, and if it has, how can I be sure if the dictionary has the existing key?


Solution

  • If you need to associate objects in your model with cells, don't make the association between the cell directly because cells are reused constantly. Instead, associate it with the index path.

    Assuming cellToObjectMap is an NSMutableDictionary, then try:

    [cellToObjectMap setObject:[managedObject valueForKey:@"test1_id"] forKey:indexPath];
    

    In general, though, you should just use the indexPath to access the object directly out of the data structures you have built to represent your table.