Iphone 更改UITableView中sectiontitle的背景色
我有一个Iphone 更改UITableView中sectiontitle的背景色,iphone,uitableview,xcode4.5,Iphone,Uitableview,Xcode4.5,我有一个UITableView,其中包含节,我正在titleForHeaderInSection中为节设置标题,如下所示: - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [NSString stringwithFormat :@"Section %d",section]; } 默认为灰色背景,文本颜色为白色。如何将其更改为其他颜色 通过
UITableView
,其中包含节,我正在titleForHeaderInSection
中为节设置标题,如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringwithFormat :@"Section %d",section];
}
默认为灰色背景,文本颜色为白色。如何将其更改为其他颜色
通过谷歌搜索,发现在viewForHeaderinSection
中设置。
但是我没有用它来设置标题,所以我不想在里面写
如何将其写入标题部分?使用以下代码可能会对您有所帮助:)
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == integerRepresentingYourSectionOfInterest)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
和
- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 25;
}
使用以下代码可能对您有所帮助:) 和
- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 25;
}
titleForHeaderInSection
:此UITableView
的委托仅用于设置文本。您可以在下面看到它的返回类型itNSString
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// fixed font style. use custom view (UILabel) if you want something different
}
为了实现节标题的自定义视图,您唯一的选项是viewForHeaderInSection
delegate方法。我可以保证,您无法使用titleForHeaderInSection
设置标题视图,viewForHeaderInSection
的作用与titleForHeaderInSection
的作用完全相同,您可以看到如下实现:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Customized your view however you want.
return myCustomizedView;
}
titleForHeaderInSection
:此UITableView
的委托仅用于设置文本。您可以在下面看到它的返回类型itNSString
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// fixed font style. use custom view (UILabel) if you want something different
}
为了实现节标题的自定义视图,您唯一的选项是viewForHeaderInSection
delegate方法。我可以保证,您无法使用titleForHeaderInSection
设置标题视图,viewForHeaderInSection
的作用与titleForHeaderInSection
的作用完全相同,您可以看到如下实现:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Customized your view however you want.
return myCustomizedView;
}
在iOS 6及更高版本中
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont systemFontOfSize:18.0f]];
[[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setBackgroundColor:[UIColor redColor]];
在iOS 6及更高版本中
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont systemFontOfSize:18.0f]];
[[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setBackgroundColor:[UIColor redColor]];
回答不错但无法显示标题:(回答不错但无法显示标题:(