Ios UICollectionView和在单元格上写入文本
我使用Ios UICollectionView和在单元格上写入文本,ios,uiscrollview,uicollectionview,uicollectionviewcell,uicollectionviewdelegate,Ios,Uiscrollview,Uicollectionview,Uicollectionviewcell,Uicollectionviewdelegate,我使用UICollectionView创建了一个多列表。如第一张图所示,它工作良好 我创建的每一行代表不同的部分,即第一行是第0部分,第二行是第1部分,等等 我需要为每一行写文本。我可以写第一张图中显示的文本 但问题是,一旦CollectionView被滚动,在第0节(第0行)中写入的文本将以不同的顺序在另一行(通常是滚动前隐藏的行)中重复,如第二幅图所示 我的代码如下所示 - (UICollectionViewCell *)collectionView:(UICollectionView *)
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
如何防止一行中的文本在另一行中不重复,并始终保持在滚动的专用行中
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
谢谢
我解决这个问题的方法是使用
didendisplayingcell
并将@写入单元格文本
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(CustomCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
cell.cellText.text = @"";
}