gpt4 book ai didi

ios - 如何为特定节标题设置背景颜色?

转载 作者:行者123 更新时间:2023-11-28 18:40:53 25 4
gpt4 key购买 nike

我有一个 TableView,其中包含从 plist 文件加载的 10 个部分,并且我有一些开关可以关闭某些部分。我需要为每个部分设置特定的背景颜色,因为该部分可以被禁用。

例子:

对于 Black 部分,我需要设置 black background

对于 Red 部分,我需要设置 red background

等等……

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,44)];
tempHeaderView.backgroundColor=[UIColor blackColor];

[tempHeaderView addSubView: tempHeaderLabel];
return tempHeaderView;
}

最佳答案

保留 UIColor 对象的 NSArray 作为类的实例变量(充当委托(delegate)/数据源的 View Controller ),假设您称它为 部分颜色。您可以根据 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];

(上面的代码应该放在 TableView 数据源的初始化器中)

关于ios - 如何为特定节标题设置背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11077808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com