将一个对象作为类NSArray
的UIColor
实例变量(充当委托/数据源的视图控制器),假设您调用它sectionColors
。您可以从 plist 中的值初始化此数组中的颜色,或对颜色进行硬编码。
然后,使用以下代码:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,44)];
// This changed:
tempHeaderView.backgroundColor = [sectionColors objectAtIndex:section];
[tempHeaderView addSubView: tempHeaderLabel];
return tempHeaderView;
// Use 'return [tempHeaderView autorelease];' in a non-ARC environment
}
这是您可以初始化数组的方式:
// Assuming your table has three sections (indices 0 through 2)
UIColor* colorForSection0 = [UIColor colorwithRed:redValue0 green:greenValue0 blue:blueValue0 alpha:1.0];
// redValue0, etc. are floats between 0.0 and 1.0 that you can read from a .plist
// Alternatively, store them as integers between 0 and 255, and divide them by 255.0
// and store on CGFloat variables before creating color.
// ...Do the same for the other colors...
// Now that you have the colors, create array and store in ivar 'sectionColors'
sectionColors = [[NSArray alloc] initWithObjects:
ColorforSection0, ColorForSection1, colorForSection2, nil];
(上面的代码应该放在表视图数据源的初始化程序中)