UICollectionViewCell改变行单元格(行)的背景?

时间:2014-04-04 11:27:35

标签: ios objective-c 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

2 个答案:

答案 0 :(得分:0)

只写这个

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

答案 1 :(得分:0)

为什么不让cellArray中的每条记录成为一个部分,然后检查[selectedIndexPath section],然后检查cellForItemAtIndexPath: [indexPath section]是否等于{{1}您可以为此部分的所有行着色

祝你好运