Ios 如何在customcell中使用";“无效”;didSelectItemAtIndexPath
我有这个问题。将UICollectionView与我的类“myCustomCell”一起使用,我在其中插入了必须更改的标签。当我使用该方法时:Ios 如何在customcell中使用";“无效”;didSelectItemAtIndexPath,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有这个问题。将UICollectionView与我的类“myCustomCell”一起使用,我在其中插入了必须更改的标签。当我使用该方法时: -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = @"Cell"; CustomCel
-(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);
}
}
如何修复?我现在明白了,您在
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);
}
}
您只需要使用前面的方法获取单元对象-
CustomCell *cell = [self collectionView:collectionView cellForItemAtIndexPath:indexPath];
使用void是什么意思?使用此方法时,您应该使用didSelectItemAtIndexPath“void”,如下:-(void)CollectionView:(UICollectionView*)CollectionView didSelectItemAtIndexPath:(NSIndexPath*)InExpath我缺少一些东西。。。我不知道要更改什么,在哪里更改,我不工作…我正在尝试,但说:不兼容的指针类型初始化“CustomCell*”和类型为“UICollectionViewCell*”的表达式我忘记添加第一行的强制转换,请参阅我的编辑:CustomCell*cell=(CustomCell*)[collectionView cellForItemAtIndexPath:indexPath];好的,清楚了,完成了。但是你只能接受一个答案,对吗?