UICollection在单元格

时间:2015-12-10 06:58:35

标签: ios uiscrollview uicollectionview uicollectionviewcell uicollectionviewdelegate

我使用UICollectionView创建一个多列表。它的效果很好,如第一张图所示。

我创建的每行代表不同的部分,即第一行是第0部分,第二行是第1部分,等等。

我需要为每一行写文字。我可以写第一张图片中显示的文字。

但问题是,一旦滚动了CollectionView,第0部分(第0行)中写入的文本将以不同的顺序在另一行(通常是滚动前隐藏的行)中重复,如第二张图片所示。

我的代码在下面以书面文字显示。

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

        UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        //Label
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];
        textLabel.font = [UIFont fontWithName:@"ArialMT"  size:12];

        textLabel.clipsToBounds = YES;
        textLabel.backgroundColor = [UIColor clearColor];
        textLabel.textColor = [UIColor blackColor];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        textLabel.numberOfLines = 0;

        if(indexPath.section == 0){
            cell.backgroundColor=[UIColor blackColor];
            textLabel.textColor = [UIColor whiteColor];
            if(indexPath.item == 0){
                textLabel.text = @"Date";

            }
            else if(indexPath.item == 1)
                textLabel.text = @"Age";
            else if(indexPath.item == 2)
                textLabel.text = @"Height (cm)";
            else if(indexPath.item == 3)
                textLabel.text = @"%le";
            else if(indexPath.item == 4)
                textLabel.text = @"Weight (kg)";
            else if(indexPath.item == 5)
                textLabel.text = @"%le";

        }
        else if(indexPath.section % 2 == 0)
            cell.backgroundColor=[UIColor grayColor];
        else
            cell.backgroundColor=[UIColor lightGrayColor];
        [cell addSubview:textLabel];


        return cell;


}

这意味着总是在滚动时调用此回调函数。

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

如何防止连续文本在另一行中重复,并且在滚动时始终保留在专用行中。

由于

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

我遇到此问题的方法是使用didEndDisplayingCell并将@“”写入单元格的文本。

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(CustomCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    cell.cellText.text = @"";

}