Чтобы установить случайный цвет текста для каждой ячейки, вы можете сначала создать функцию для генерации случайных цветов, подобных этому.
- (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;
}
Что ваш код в 'collectionView: cellForItemAtIndexPath:'? Поместите туда какую-то логику. – Larme
Вам нужно показать нам, что вы пробовали? –
@ hd1344 Можете ли вы поделиться кодом? для разного цвета текста, в cellForItemAtindexPath: вам нужно передать другой цвет для разных индексов. Ex - if (indexPath.row == 1) {// передаем цвет текста здесь} – Developer