1

I create a multi-column table using UICollectionView. It works well as shown in the first picture.

I created as each row represents different sections, i.e. the first row is section 0, the second row is section 1, etc.

I need to write text for each row. I can write text as shown in the first image.

But the problem is, once the CollectionView is scrolled, the text written in section 0 (row 0) is repeated in another row (normally is the row hidden before scrolling) in different order as shown in the second image.

My code is shown below in writing text.

- (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;


}

That means this callback function is always called in scrolling.

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

How can I prevent Text in a row is not repeated in another row and always stay in the dedicated row in scrolling.

Thanks

enter image description here

enter image description here

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
batuman
  • 7,066
  • 26
  • 107
  • 229

1 Answers1

1

My solution for this problem is using didEndDisplayingCell and write @"" to cell's text.

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

}
batuman
  • 7,066
  • 26
  • 107
  • 229