簡體   English   中英

無法在 uicollectionview、UICollectionView 的特定 indexPath.row 處為單元格設置標題

[英]can not set title for cell at specific indexPath.row of uicollectionview, UICollectionView

在 UICollectionView 上工作時,我正在從 nib 加載一個單元格,如下所示

- (void)viewDidLoad
{
    [super viewDidLoad];
    /* Uncomment this block to use nib-based cells */
    UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];


    // Configure layout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView.contentInset    =   UIEdgeInsetsMake(10.0f, 0.0f, 10.0f, 0.0f);

    [flowLayout setItemSize:CGSizeMake(300, 200)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:flowLayout];
}

我的 nib 文件如下所示

在此處輸入圖片說明

頂部標簽用於顯示單元格數量,中間標簽用於顯示某個文本

在方法cellForItemAtIndexPath 中,我只想將文本設置為特定行的單元格(在本例中,我在第 1 行執行此操作):

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

     // Setup cell identifier
     static NSString *cellIdentifier = @"cvCell";

     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
     UILabel *titleLabel    = (UILabel *)[cell viewWithTag:100];
     UILabel *cellLablel    = (UILabel *)[cell viewWithTag:200];
     cellLablel.text         =   self.dataArray[0][indexPath.row];
     if ( indexPath.row == 1)
         [titleLabel setText:@"this is row 1"];

    return cell;

}

運行該應用程序時,出現問題。 不僅row1的單元格的titleLabel設置為This is row 1 ,而且 row5 和 row9 和 row10 的 titleLabel 也設置為: 在此處輸入圖片說明

在此處輸入圖片說明

如果有人知道我在中間做錯了什么。 請幫忙。

我的收藏包含 1 個部分和此部分的 15 行。

細胞被重復使用。 一旦您向下滾動並且 Cell 1 變得不可見,集合視圖就會使其再次可用。 只需為所有單元格設置文本,例如

titleLabel.text = [NSString stringWithFormat:@"this is row %d", indexPath.row];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM