1

我有这个问题。将 UICollectionView 与我的类“myCustomCell”一起使用,我在其中插入了一个我必须更改的标签。当我使用该方法时:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    if (indexPath.section == 0) {
        cell.backgroundColor = [self colorForIndexToBlack:indexPath.item];
        UIColor *colorCell = cell.backgroundColor;
        NSString *strColorCell = [NSString stringWithFormat:@"%@", colorCell.hexStringValue];
        cell.lblCustomCell.text = [NSString stringWithFormat:@"HEX %@", strColorCell];
    } else {
        cell.backgroundColor = [self colorForIndexToWhite:indexPath.item];
        UIColor *colorCell = cell.backgroundColor;
        NSString *strColorCell = [NSString stringWithFormat:@"%@", colorCell.hexStringValue];
        cell.lblCustomCell.text = [NSString stringWithFormat:@"HEX %@", strColorCell];
    }

    return cell;
}

lblCustomCell(这是我要更改的标签)充满了我想要的信息,没关系。但是当我使用这种方法“void”来选择单个单元格并编辑标签的文本时,它不起作用并且不会用我的新文本更改标签。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    if (indexPath.section == 0) {
        cell.backgroundColor = [self colorForIndexToBlack:indexPath.item];
        UIColor *colorCell = cell.backgroundColor;
        NSString *strColorCell = [NSString stringWithFormat:@"%@", colorCell.hexStringValue];
        cell.lblCustomCell.text = strColorCell;
        NSLog(@"Indice della cella: %i %i %@", indexPath.item, indexPath.section * 2 + indexPath.row, strColorCell);
    } else {
        cell.backgroundColor = [self colorForIndexToWhite:indexPath.item];
        UIColor *colorCell = cell.backgroundColor;
        NSString *strColorCell = [NSString stringWithFormat:@"%@", colorCell.hexStringValue];
        cell.lblCustomCell.text = strColorCell;
        NSLog(@"Indice della cella: %i %i %@", indexPath.item, indexPath.section * 2 + indexPath.row, strColorCell);
    }
}

怎么能修?

4

2 回答 2

0

我现在看到,您没有在didSelectItemAtIndexPath方法中正确获取单元格。要获取单元格,请使用以下命令:

- (UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;

并更改您想要的任何内容,例如:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
     CustomCell *cell = (CustomCell *)[collectionView cellForItemAtIndexPath:indexPath];
      if (indexPath.section == 0) {
         cell.backgroundColor = [self colorForIndexToBlack:indexPath.item];
         UIColor *colorCell = cell.backgroundColor;
         NSString *strColorCell = [NSString stringWithFormat:@"%@",     colorCell.hexStringValue];
        cell.lblCustomCell.text = strColorCell;
         NSLog(@"Indice della cella: %i %i %@", indexPath.item, indexPath.section * 2 + indexPath.row, strColorCell);
     } else {
         cell.backgroundColor = [self colorForIndexToWhite:indexPath.item];
         UIColor *colorCell = cell.backgroundColor;
         NSString *strColorCell = [NSString stringWithFormat:@"%@", colorCell.hexStringValue];
         cell.lblCustomCell.text = strColorCell;
         NSLog(@"Indice della cella: %i %i %@", indexPath.item, indexPath.section * 2 + indexPath.row, strColorCell);
     }
}
于 2013-10-10T09:59:33.487 回答
0

您只需要使用前面的方法获取您的单元格对象。-

CustomCell *cell = [self collectionView:collectionView cellForItemAtIndexPath:indexPath];
于 2013-10-10T10:00:23.310 回答