【问题标题】:UICollectionView and writing text on cellsUICollectionView 和在单元格上写入文本
【发布时间】:2015-12-10 06:58:34
【问题描述】:

我使用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

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

谢谢

【问题讨论】:

    标签: ios uiscrollview uicollectionview uicollectionviewcell uicollectionviewdelegate


    【解决方案1】:

    我对这个问题的解决方案是使用didEndDisplayingCell 并将@"" 写入单元格的文本。

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

    【讨论】:

      猜你喜欢
      • 2020-05-30
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多