[分享]iOS开发 - 怎样在UITableView里修改section header的颜色

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了[分享]iOS开发 - 怎样在UITableView里修改section header的颜色脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1.希望这个从UITableViewDelegate协议里得到的方法可以对你有所帮助:

-(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;
}

使用任何你喜欢UIColor代替[UIColor redColor]。你可能还希望调整headerView的尺寸

2.这是改变文本颜色的方法:

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithred:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];


3.不要忘记从委托添加这段代码,否则在某些情况下视图将被切断或者出现在table后面,相对于视图/标签的高度。

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

4.如果你想自定义header颜色,可以这样做

[[UITableViewHeaderFooterView apPEarance] setTintColor:[UIColor redColor]];

这个方法在iOS 6.0.以上都很好用。

5.这是在标题视图添加图片的方法:

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
    UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];
 
    headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);
 
    [headerView addSubview:headerImage];
 
    return headerView;
}

6.如果你不想建立自定义视图,你也可以这样改变颜色:

-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if ([view isKindOfclass: [UITableViewHeaderFooterView class]]) {
        UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
        UIView* content = castView.contentView;
        UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
        content.backgroundColor = color;
    }
}

7.这个方法不涉及定义和创建自定义视图。在iOS 6以上,你可以通过以下方法轻松改变背景色和文本色:

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];
 
    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];
 
    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}

注:通过UITableViewHeaderFooterView设置背景色的方法已经被废弃了。请用contentView.backgroundColor代替。


分享来
http://stackoverflow.com/ques...

脚本宝典总结

以上是脚本宝典为你收集整理的[分享]iOS开发 - 怎样在UITableView里修改section header的颜色全部内容,希望文章能够帮你解决[分享]iOS开发 - 怎样在UITableView里修改section header的颜色所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。