2013-10-10 18 views
1

我有這個問題。使用UICollectionView與我的類「myCustomCell」插入我必須更改的標籤。當我使用方法:如何更改我的標籤在customcell中使用「void」didSelectItemAtIndexPath

-(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

當你使用void時,你是什麼意思? –

+0

使用此方法,您應該使用didSelectItemAtIndexPath「void」,如下所示: - (Void)CollectionView:(UICollectionView *)CollectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath –

回答

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); 
    } 
} 
+0

我錯過了一些東西...我不明白要更改什麼和我不工作... –

+0

看到我的編輯,改變你的方法這個 –

+0

我想,但說:不兼容的指針類型初始化'CustomCell *'與'UICollectionViewCell *'類型的表達'' –

0

你只需要與以往method.-

CustomCell *cell = [self collectionView:collectionView cellForItemAtIndexPath:indexPath]; 
讓你的對象