每次重新加载调用时都会更新UICollectionViewCell数据

时间:2017-05-05 23:48:33

标签: ios objective-c xcode

我需要UICollectionView中的单元格,它将显示文本或图像。

我通过创建自己的UICollectionViewCell来实现它,它有两个属性--UIImage属性和NSString属性。

根据调用者设置的属性,我会显示图像或文本。调用集合视图也使用以下方法重用单元格:

cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

但每当我在我的代码中重新加载集合视图时,单元格都会搞砸 - 它们显示空按钮。我究竟做错了什么?如果您需要我发布任何其他特定代码,请告诉我。我不想用无用的代码弄乱我的问题。

在我的CollectionViewCell中,我正在重置标签和图像:

-(void) prepareForReuse {
    [super prepareForReuse];
    [_button setTitle:@"" forState:UIControlStateNormal];
    [_button setImage:nil forState:UIControlStateNormal];
    [_button removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
}

cellForItemAt代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    NSInteger position = indexPath.row;

    long listIdx = position - 1;

    if (listIdx == ADD_ICON_INDEX) {

        UIImage * addImage = [self _getAddImage];
        [cell setButtonImage:addImage target:self action:@selector(_addButtonClicked:)];
        cell.button.tag = listIdx;
        return cell;

    }

    NSString *title= _visibleButtons[listIdx];

    [cell setButtonTitle:title target:self action:@selector(_buttonClicked:)];
    cell.backgroundColor = [UIColor clearColor];
    cell.button.tag = listIdx;


    return cell;

}

0 个答案:

没有答案