【问题标题】:iOS: UITableView cell.tag number keeps increasing when in and out of the ViewiOS:进出视图时,UITableView cell.tag 数量不断增加
【发布时间】:2013-08-16 02:24:34
【问题描述】:

如果单元被分配了标签号,我如何跟踪?我将创建 10 到 90 个单元格。但是,当我上下滚动(进出视图)时,重新创建单元格后,单元格标记号继续增加。

- (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;

}

日志:

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

如果一个单元格编号已经分配了一个 cell.tag,我如何跟踪它,如果有,我如何确定字典是否有现有的键?

【问题讨论】:

    标签: ios tags uitableview cell


    【解决方案1】:

    您只需要为每个新单元格分配一个标签,对吗?

    像这样定义一个静态标签

    #define CELL_TAG 100000
    

    现在在你的 cellForRowAtIndexPath 中

    cell.tag = indexPath.row + CELL_TAG
    

    您添加的后缀表达式将始终增加标签,因此也将其删除。

    PS。单元初始化在哪里?如果你需要的话

    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    

    【讨论】:

      【解决方案2】:

      如果您需要将模型中的对象与单元格关联,请不要直接在单元格之间建立关联,因为单元格会不断重复使用。而是将其与索引路径相关联。

      假设cellToObjectMapNSMutableDictionary,那么试试:

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

      不过,一般来说,您应该只使用indexPath 直接从您为表示表而构建的数据结构中访问对象。

      【讨论】:

      • 卡尔又来救援了!有效,但 IndexPath 分配的值如下所示:2013-08-15 19:48:45.735 Time[8711:c07] IndexPath 分配值: 2 个索引 [0, 31] 分配该值是否可以钥匙?此外,我收到不兼容的指针类型将 NSIndexPath*_ strong 发送到类型为“NSString *”警告的参数
      • :) cellToObjectMap 是可变字典吗?或者是别的什么?如果是,您使用的是setObject:forKey: 还是setValue:forKey:
      • 它是一个可变字典。我正在使用 setValue,然后将其更改为 setObject。在一个新部分之后,indexPath 重置为 0。我将按照您对 stackoverflow.com/questions/18243612/… 的建议进行操作。您如何建议我跟踪每个单元格并将其映射到 objectID。我需要跟踪 didSelectRowAtIndexPath: 并将该对象传递给下一个 UITableViewController 以显示其余的键/值。谢谢。
      • 如果只需要获取与索引路径关联的对象,那么再次使用截面模型,如CVSectionModel *sectionModel = self.sectionModels[indexPath.section]; NSManagedObject *object = sectionModel.rowModels[indexPath.row];。还要查看您的代码,您是否维护了一些单独的 rowModels 数组?您应该只跟踪部分,因为 rowModel 会根据您所在的部分而有所不同,而且您真的不想跟踪那种状态。
      • 哦,好的,太好了 :) 一般的教训是永远不要试图维护额外的标志和标签,当你可以让模型隐式表示所有这些时,不要尝试维护额外的标志和标签,它可以节省大量代码!
      猜你喜欢
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多