Question

I need to update each cell with a tag id. I have completed this, but there is one crucial flaw. When I change the order of the cells, the cells will inherit the wrong id.For example,if I swap cell 2 for cell 1 , cell 1 will get cell 2 id and cell 2 will get cell 1 id. Each ID needs to be for a specific cell and one cell only so that way if I changed the order the cell it will contain its id. I believe the issue is I am calling id in my UICollectionView delegate and so that way every time it updates it call from the server and reuses the id again. How can I get around this?

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

No correct solution

OTHER TIPS

What you can do is read the cell id first, and then see if that tag id matches an id in your list. If it doesn't, assign it an id (assuming first load of that particular cell or the whole table view in general). If it does, do not assign a new value. I know this method is not particularly safe if your assignable id values are in the range of the table view. In that case, use a hash or large numbered id or one that is a concatenation of filler ints like id[1] = 1001 and id[2] = 1002.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top