Changer la couleur d'arrière-plan de la section UITable sans perdre le titre de la section
-
29-10-2019 - |
Question
J'ai changé la backgroundColor / backgroundImage de mes sections tableviews.
J'utilise plain tableView.
Pas de soucis, travaillez sans problème avec ce code:
-(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;
}
Le problème est maintenant que le titre de la section a disparu.
Je suis en train de définir le titre de la section avec ce code:
- (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];
}
}
Je reçois les titres des sections si je ne les utilise pas
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Besoin de votre aide.
Merci beaucoup.
MODIFIER 1:
Merci à Mutix:
dans mon cas, la réponse serait:
-(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;
}
La solution
Le viewForHeaderInSection
remplace la méthode titleForHeaderInSection
.
Pour ajouter un titre à votre en-tête lorsque vous utilisez viewForHeaderInSection
, vous devrez ajouter une étiquette à la vue d'en-tête de section comme ceci:
-(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;
}
Autres conseils
Nous avons une meilleure solution pour cela à partir d'iOS6:
Il existe une méthode déléguée
- (void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)section
Vous pouvez utiliser cette méthode comme ceci
-(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]];
}
Implémentez la méthode ci-dessous à partir de UITableViewDelegate et la vue de l'interface utilisateur nécessaire.
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UITableViewHeaderFooterView()
view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
return view
}
Si vous avez besoin de modifier le pied de page, utilisez un code similaire dans la méthode ... viewForFooterInSection ..
Vous voulez dessiner votre propre police, utilisez une personnalisation similaire:
...
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
...
Ajoutez simplement [titleLabel setBackgroundColor:[UIColor clearColor]];
car dans mon cas, l'étiquette chevauchait la couleur verte.
~Best Solution iPhone 3.5 and 4 screen~
Set the back color or background image from this code. Here you don't have to calculate for section height, just set in 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;
}