2017-01-06 2 views
-3

Как установить другой цвет фона для каждой ячейки в UICollectionViewCell.Как установить различный цвет текста для каждой ячейки в UICollectionView?

UICollectionView Screenshot

  1. Текст должен быть в разных colour.like (один = красный, зеленый = два, как разумному).
  2. Я хочу, чтобы последняя ячейка «выбрала все». Я хочу эту ячейку без
  3. любой теневой эффект. Только Просмотреть все Должно быть в центре.
+0

Что ваш код в 'collectionView: cellForItemAtIndexPath:'? Поместите туда какую-то логику. – Larme

+2

Вам нужно показать нам, что вы пробовали? –

+0

@ hd1344 Можете ли вы поделиться кодом? для разного цвета текста, в cellForItemAtindexPath: вам нужно передать другой цвет для разных индексов. Ex - if (indexPath.row == 1) {// передаем цвет текста здесь} – Developer

ответ

1

Чтобы установить случайный цвет текста для каждой ячейки, вы можете сначала создать функцию для генерации случайных цветов, подобных этому.

- (UIColor *)getRandomColor { 
    CGFloat hue = (arc4random() % 256/256.0); 
    CGFloat saturation = (arc4random() % 128/256.0) + 0.5; 
    CGFloat brightness = (arc4random() % 128/256.0) + 0.5; 
    UIColor *randomColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; 
    return randomColor; 
} 

затем внутри -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath вы можете установить цвет для текстовой метки, как так

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 

    // you can do it inside the cell's subclass file also, which might be better 
    cell.titleLabel.textColor = [self getRandomColor]; 
    cell.titleLabel.textAlignment = NSTextAlignmentLeft; 


    // as for question 3 
    // quick way to achieve this would be to check if its the last row 
    // and set the text alignment to center. 
    // but calling `count` for every cell has a performance hit 
    // so you can save it into a variable outside the delegate 

    if (indexPath.row == [dataArray count] - 1]) { 
     cell.titleLabel.textAlignment = NSTextAlignmentCenter; 
    } 

    return cell; 
} 
-1

Если вы хотите, чтобы обеспечить определенный цвет по каждому индексу ячейки, то вы можете обеспечить кожух переключателя и применить цвет to text in - (UICollectionViewCell *) collectionView: (UICollectionView *) collectionView cellForItemAtIndexPath: (NSIndexPath *) метод indexPath иначе использует случайный цвет, вызывая некоторую функцию для получения случайного цвета.

+0

Это даже не пытается ответить на вопрос OP –