【问题标题】:Change UITable section backgroundColor without loosing section Title更改 UITable 部分背景颜色而不丢失部分标题
【发布时间】:2012-01-14 20:28:51
【问题描述】:

我已经更改了我的 tableviews 部分的 backgroundColor/backgroundImage。
我正在使用普通的tableView。 不用担心,使用此代码毫无问题地工作:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//For using an image use this:
//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

return sectionView;
}  

现在的问题是,章节标题不见了。

我正在使用此代码设置部分的标题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    return @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}


}

如果我不使用,我会得到章节标题

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  

需要你的帮助。
非常感谢。

编辑 1:

感谢 Mutix:

就我而言,答案是:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {




UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 20)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

//Add label to view
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 20)];
titleLabel.backgroundColor = [UIColor clearColor];

//section Title
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    titleLabel.text = @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    titleLabel.text = [sectionInfo name];
}



[sectionView addSubview:titleLabel];
[titleLabel release];

return sectionView;

}

【问题讨论】:

    标签: ios title background-color sectionheader


    【解决方案1】:

    从 UITableViewDelegate 实现下面的方法,需要 ui 视图。

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UITableViewHeaderFooterView()
        view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
        return view
    }
    

    如果您需要更改页脚,请在 ...viewForFooterInSection.. 方法中使用类似的代码

    想画自己的字体,使用类似的自定义:

    ...    
    let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight))
    label.font = UIFont(name: "HelveticaNeue", size: 14)!
    label.text = self.tableView(tableView, titleForHeaderInSection: section)
    view.contentView.addSubview(label)
    
    view.textLabel.hidden = true
    ...
    

    【讨论】:

      【解决方案2】:

      从 iOS6 开始,我们有更好的解决方案:

      有一个委托方法

      - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

      你可以这样使用这个方法

      -(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
      
           //Set the background color of the View
           view.tintColor = [UIColor blueColor];
      
           // Text Color
           UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
           [header.textLabel setTextColor:[UIColor whiteColor]];
      
      }
      

      【讨论】:

        【解决方案3】:

        ~最佳解决方案 iPhone 3.5 和 4 屏幕~

        从此代码设置背景颜色或背景图像。这里不用计算截面高度,在xib中设置即可。

        -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
            
            UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,tableView.sectionHeaderHeight)];
            sectionView.backgroundColor = [UIColor greenColor];
            
            return sectionView;
        }
        

        【讨论】:

          【解决方案4】:

          只需添加[titleLabel setBackgroundColor:[UIColor clearColor]];
          ,因为在我的例子中,标签与绿色重叠。

          【讨论】:

            【解决方案5】:

            viewForHeaderInSection 覆盖 titleForHeaderInSection 方法。

            要在使用viewForHeaderInSection 时向标题添加标题,您需要向部分标题视图添加标签,如下所示:

            -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
            
                UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];
            
                sectionView.backgroundColor = [UIColor greenColor];
            
                //Add label to view
                UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 40)];
                titleLabel.text = @"title";
                [sectionView addSubview:titleLabel];
                [titleLabel release];
            
                return sectionView;
            }  
            

            【讨论】:

            • 从 iOS 开始,我们可以使用 - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 来达到这个目的。我已经在下面解释了我的答案。
            • 澄清:从iOS6开始
            猜你喜欢
            • 2011-04-12
            • 2013-01-03
            • 2023-03-04
            • 2011-11-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-26
            • 2019-11-13
            相关资源
            最近更新 更多