保持UICollectionView Cell的值

时间:2015-02-08 14:11:33

标签: objective-c uicollectionview

UICollectionViewCell中有一个标签。 我在UICollectionView中创建了6个单元格,并为每个单元格中的标签分配了一个数字(1-6)。 我调用[UICollectionView reloadData]并期望在标签中使用相同的数字。 但是,当调用cellForItemAtIndexPathas时,标签值会发生变化,例如" 1 3 1 3 1 3"。 似乎UICollectionViewCell在cellForItemAtIndexPathas中重新定位。

有没有办法保留它?

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    InfoCollectionViewCell *infoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InfoCell" forIndexPath:indexPath];

    [infoCell.cellText setText:[infoCell.cellText.text stringByAppendingString:[NSString stringWithFormat:@"%d\n",indexPath.row]]];

    return infoCell;
}

1 个答案:

答案 0 :(得分:0)

您添加到单元格的标签存在重用问题。

我发了一篇关于如何正确地继承UICollectionViewCell的帖子。 你可以在这里查看: UICollectionView adding image to a cell