2014-04-04 25 views
0

如何選擇一個單元格 - 更改行單元格(行)的背景? 我不知道如何獲得行線單元格的索引路徑。一個選擇單元格改變顏色沒有任何問題。 我需要改變整個行的背景。請。幫我UICollectionViewCell更改行單元格(行)的背景?

Array *cellArray = @[ @[@0, @0, @0, @2, @6, @0, @7, @0, @1], 
        @[@6, @8, @0, @0, @7, @0, @0, @9, @0], 
        @[@1, @9, @0, @0, @0, @4, @5, @0, @0], 
        @[@8, @2, @0, @1, @0, @0, @0, @4, @0], 
        @[@0, @0, @4, @6, @0, @2, @9, @0, @0], 
        @[@0, @5, @0, @0, @0, @3, @0, @2, @8], 
        @[@0, @0, @9, @3, @0, @0, @0, @7, @4], 
        @[@0, @4, @0, @0, @5, @0, @0, @3, @6], 
        @[@7, @0, @3, @0, @1, @8, @0, @0, @0]]; 

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

      //code 

        if ([self.selectedIndexPath isEqual:indexPath]) { 

          cell.backgroundColor = [UIColor redColor]; 

          // set highlight color 
         } else { 
          cell.backgroundColor = [UIColor grayColor]; 
          // set default color 
         } 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSArray *arrayOfIndexPaths = [self.collectionView indexPathsForSelectedItems]; 

    if (indexPath && [self.board isEditableFieldAtIndex:indexPath.row]) { 
     NSMutableArray *indexPathsToReload = [@[indexPath] mutableCopy]; 
     if (self.indexPathForLastCellMarkedEditing && ![self.indexPathForLastCellMarkedEditing isEqual:indexPath]) { 
      // if one cell was already marked, we have to unmark it 
      [indexPathsToReload addObject:self.indexPathForLastCellMarkedEditing]; 
     } 

     self.indexPathForLastCellMarkedEditing = indexPath; 
     [self.collectionView reloadItemsAtIndexPaths:indexPathsToReload]; 
    } 
    else { 
     [self unmarkCells]; 
    } 

    self.indexPathForLastCellMarkedEditing = indexPath; 
    [self.collectionView reloadItemsAtIndexPaths:arrayOfIndexPaths]; 
    [self.collectionView reloadData]; 
} 

image

+1

你didSelectItemAtIndexPath顏色的所有行? ????? –

+0

Tis是CollectionView,因此您可以更改整行的顏色...您只更改所選項目的背景顏色。 –

+0

是的,我使用didSelectItemAtIndexPath,但我只能更改一個單元格backgroundColor。我如何獲得整行的indexpatch? – Fabio

回答

0

寫這個只是

if (cell.selected) 
    { 
      cell.backgroundColor = [UIColor redColor]; // highlight selection 
    } 
    else 
    { 
      cell.backgroundColor = [UIColor grayColor]; // Default color 
    } 
+0

由於顏色只改變點擊單元格。當你點擊一行(行)中的一個單元時,我需要這個 - 從這行(行)改變顏色。我怎樣才能改變靠近細胞的顏色? – Fabio

0

你爲什麼不使cellArray每個記錄是一款和檢查[selectedIndexPath section]然後在cellForItemAtIndexPath:如果[indexPath section]等於[selectedIndexPath section]你可以在本節

好運

+0

在最後一種情況下,您應該在所選單元格的交叉點垂直和水平更改背景線。 – Fabio

相關問題