我有这个问题。使用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);
}
}
如何解决?
答案 0 :(得分: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);
}
}
答案 1 :(得分:0)
您只需要使用以前的方法获取您的单元格对象.-
CustomCell *cell = [self collectionView:collectionView cellForItemAtIndexPath:indexPath];