UITableView - 更改节标题颜色 [英] UITableView - change section header color

查看:165
本文介绍了UITableView - 更改节标题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在UITableView中更改区段标题的颜色?

How can I change color of a section header in UITableView?

EDIT 由DJ-S提供的答案应考虑用于iOS 6及更高版本。接受的答案已过期。

EDIT: The answer provided by DJ-S should be considered for iOS 6 and above. The accepted answer is out of date.

推荐答案

希望此方法从 UITableViewDelegate 协议将让您开始:

Hopefully this method from the UITableViewDelegate protocol will get you started:

objC:

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

swift: b

func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
  let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
  if (section == integerRepresentingYourSectionOfInterest) {
    headerView.backgroundColor = UIColor.redColor()
  } else {
    headerView.backgroundColor = UIColor.clearColor()
  }
  return headerView
}

替换 [UIColor redColor] 以您 UIColor 为准。您也可以调整 headerView 的尺寸。

Replace [UIColor redColor] with whichever UIColor you would like. You may also wish to adjust the dimensions of headerView.

这篇关于UITableView - 更改节标题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆