iOS:如何在此方法中为每个单元格分配标记?

iOS:如何在此方法中为每个单元格分配标记?,ios,iphone,objective-c,uicollectionview,cell,Ios,Iphone,Objective C,Uicollectionview,Cell,我需要用标签id更新每个单元格。我已经完成了这项工作,但有一个关键缺陷。当我更改单元格的顺序时,单元格将继承错误的id。例如,如果我将单元格2替换为单元格1,cell 1将获得cell 2 id,cell 2将获得cell 1 id。每个id需要用于一个特定的cell和一个cell,这样,如果我更改了单元格的顺序,它将包含其id。我认为问题在于我在UICollectionView委托中调用id,这样每次它都会从服务器更新它的调用并再次重用id。我怎样才能避开这件事 - (UICollection

我需要用标签id更新每个单元格。我已经完成了这项工作,但有一个关键缺陷。当我更改单元格的顺序时,单元格将继承错误的id。例如,如果我将单元格2替换为单元格1,cell 1将获得cell 2 id,cell 2将获得cell 1 id。每个id需要用于一个特定的cell和一个cell,这样,如果我更改了单元格的顺序,它将包含其id。我认为问题在于我在UICollectionView委托中调用id,这样每次它都会从服务器更新它的调用并再次重用id。我怎样才能避开这件事

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"Cell"];

    Cell *cell = (Cell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if (self.data == nil || [self.data count] == 0) {
        self.data = [sections objectAtIndex:indexPath.section];

        cell.cellImg.image = nil;
        [tagArray removeAllObjects];
    } else {
        self.data = [sections objectAtIndex:indexPath.section];

        cell.cellImg.image = [self.data objectAtIndex:indexPath.item];
        self.largeImage.image = [self.data objectAtIndex:0];

        NSDictionary *url  = [self.result objectAtIndex:indexPath.row];
        NSString *string = [url valueForKey:@"id"];

        cell.cellImg.tag = string.intValue;
        [tagArray addObject:[NSNumber numberWithInt:cell.cellImg.tag]];
    }
    return cell;
}

-(void)didGetResults:(NSArray *)resultArray
{
    self.resultArray = resultArray;
    resultModel = [self.resultArray objectAtIndex:0];
    self.resultdata = resultModel.resultdata;

    sections = [[NSMutableArray alloc] initWithCapacity:SECTION_COUNT];
    for(int s = 0; s < SECTION_COUNT; s++) {
        self.data = [[NSMutableArray alloc] initWithCapacity:self.resultdata.count];
        for(int i = 0; i < self.result.count; i++) {
            NSDictionary *url  = [self.result objectAtIndex:i];
            NSString *string = [url valueForKey:@"web_path"];

            UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:string]]]];

            [self.data addObject:myImageView.image];

        }
        [sections addObject:self.data];
    }
    [tagArray removeAllObjects];

    [self.collectionView reloadData];
}

您可以首先读取单元格id,然后查看该标记id是否与列表中的id匹配。如果没有,则假定第一次加载该特定单元格或整个表视图,为其分配一个id。如果是,则不要指定新值。我知道,如果可分配的id值在表视图的范围内,则此方法并不特别安全。在这种情况下,请使用散列或大编号id,或是id[1]=1001和id[2]=1002等填充整数的串联