我正在设计一个应用程序,其中我使用表视图。这里我必须同时为tableview的单元格分配3种不同的颜色...在从下一行分配这些颜色后,问题来了,我必须再次以相反的顺序分配相同的颜色。例如,我有三种颜色,我需要分配这些颜色的顺序是:红-绿-蓝-蓝-绿-红-红-绿-蓝-蓝-绿-红……诸若此类。这里假设我们不知道有多少行,这个序列应该像这样继续下去。请帮帮我!
发布于 2015-10-15 12:39:34
将下面的数组声明为全局数组:
NSMutableArray *colorArray;
ViewDidLoad
函数:
colorArray = [[NSMutableArray alloc] initWithObjects:[UIColor redColor],[UIColor greenColor],[UIColor blueColor],[UIColor blueColor], [UIColor greenColor], [UIColor redColor], nil];
cellForRowAtIndexPath
方法:
cell.backgroundColor = [colorArray objectAtIndex:indexPath.row % 5];
并完成:)。
发布于 2015-10-15 12:57:24
您需要初始化一个颜色数组并在cellForRowAtIndexPath方法中使用,声明一个数组
NSMutableArray *colors;
在did加载中
color=[[NSMutableArray alloc]init];
在viewDidLoad中调用此方法
-(void)initializeColorArray
{
UIColor *color1=[UIColor redColor];
UIColor *color2=[UIColor orangeColor];
UIColor *color3=[UIColor yellowColor];
NSArray *Listcolor = @[color1,color2,color3,color3,color2,color1];
for (int i=0; i<[tableData count]; i=i+[Listcolor count])
{
for (int i=0; i<[Listcolor count]; i++)
{
[colors addObject:[Listcolor objectAtIndex:i]];
}
}
NSLog(@"%@",color);
}
并使用颜色数组在cellForRowAtIndexPath方法中指定背景色
cell.backgroundColor=[colors objectAtIndex:indexPath.row];
它会给出与您需要的Happy Coding相同的输出。:)
发布于 2015-10-15 13:35:49
非常简单但很棘手的问题
只需将此粘贴到cellForRowAtIndexPath中
cell.backgroundColor=[self colorForIndex];
和定义方法
-(UIColor*)colorForIndex
{
UIColor *returnColor;
NSArray *colorsArray=@[[UIColor whiteColor],[UIColor redColor],[UIColor greenColor]];
NSArray *reverseColorsArray=@[[UIColor greenColor],[UIColor redColor],[UIColor whiteColor]];
if(currentSet%2==0){
returnColor =[colorsArray objectAtIndex:counter3];
}else{
returnColor =[reverseColorsArray objectAtIndex:counter3];
}
counter3++;
if(counter3==3){
currentSet++;
counter3=0;
}
return returnColor;
}
https://stackoverflow.com/questions/33139662
复制相似问题