セクションのタイトルを失うことなく、uableセクションの背景colorを変更します

StackOverflow https://stackoverflow.com/questions/8413436

質問

TableViewsセクションのバックグラウンドカラー/バックグラウンドイメージを変更しました。
Plane 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;

}

役に立ちましたか?

解決

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

他のヒント

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]];

}

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
...

追加するだけです [titleLabel setBackgroundColor:[UIColor clearColor]];
私の場合、ラベルが緑色に重なり合ったからです。

〜最良のソリューション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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top