我需要使用标记ID更新每个单元格。我已经完成了这个,但有一个至关重要的缺陷。当我改变单元格的顺序时,单元格将继承错误的id。例如,如果我将单元格2交换为单元格1,单元格1将获得单元格2 id,单元格2将获得单元格1 id。每个ID只需要用于特定的单元格和一个单元格,这样如果我改变了它将包含其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];
}
答案 0 :(得分:0)
您可以做的是首先读取单元格ID,然后查看该标记ID是否与列表中的ID匹配。如果没有,则为其分配一个id(假设首先加载该特定单元格或整个表视图)。如果是,请不要分配新值。我知道如果您的可分配id值在表视图的范围内,则此方法不是特别安全。在这种情况下,请使用散列或大号编号的id或者是id[1] = 1001
和id[2] = 1002
等填充符的串联。