不自定义也可更改UITableViewCellSection背景颜色字体颜色

来源:互联网 发布:雷达炒股软件 编辑:程序博客网 时间:2024/06/07 18:31
- (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;}[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];这个方法在iOS 6.0.以上都很好用。这是在标题视图添加图片的方法:- (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;}-(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;    }}- (void)tableView:(UITableView *)tableView         willDisplayHeaderView:(UIView *)view         forSection:(NSInteger)section
// 强力推荐- (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]];      // header.contentView.backgroundColor = [UIColor blackColor];}通过UITableViewHeaderFooterView设置背景色的方法已经被废弃了。请用contentView.backgroundColor代替。
0 0