1

Bonjour, j'ai essayé de définir une vue personnalisée sur l'en-tête de vue de table, mais la vue personnalisée ne convient pas à l'en-tête.La vue personnalisée n'est pas adaptée à l'en-tête dans UItableview

La vue personnalisée arrive comme ci-dessous, dans cette image, la vue personnalisée est de couleur orange et la vue d'en-tête est de couleur grise.Mais je veux une vue personnalisée est pleine de vue d'en-tête.

enter image description here

s'il vous plaît aider.

mycode:

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

     UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, _TableList.frame.size.width, 80)]; 
     header = [[HeaderView alloc] initWithFrame:CGRectMake(sectionView.frame.origin.x, sectionView.frame.origin.y, sectionView.frame.size.width, sectionView.frame.size.height)]; 
     [sectionView addSubview:header]; 
     sectionView.tag=section; 
     sectionView.backgroundColor = [UIColor grayColor]; 

     UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)]; 
     [sectionView addGestureRecognizer:headerTapped]; 

     return sectionView; 
    } 
+0

Je pense que vous devez utiliser autolayout pour cette – iSashok

+0

pouvez-vous s'il vous plaît me expliquer avec un code –

+0

Pourquoi êtes-vous en utilisant deux vues? Gardez juste headerView et définissez son cadre comme ceci - UIView * sectionView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, _TableList.bounds.size.width, 80)]; –

Répondre

2

Si vous utilisez Xib pour charger la vue personnalisée en tant que tête de section, puis faire le code comme ci-dessous:

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    HeaderView *headerview = [[[NSBundle mainBundle] loadNibNamed:@"SecHeader" 
                  owner:self 
                  options:nil] objectAtIndex:0]; 
    headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 80); 

    UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)]; 
    [headerview addGestureRecognizer:headerTapped]; 

    return headerview; 
} 
+0

merci, Bro.Il travaille .... –

+0

Heureux de vous aider! @Anilkumar – Jaimish

+0

@Anilkumar Si la solution de story-board vous a aidé alors upvote aussi ... Merci – Jaimish

0

dans viewForHeaderInSection

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
UILabel *labelSection; 
UIView *viewSection = [[UIView alloc]init]; 
viewSection.frame = CGRectMake(0, 0, tableview.frame.size.width, 20); 
labelSection = [[UILabel alloc]init]; 
labelSection.textAlignment = NSTextAlignmentLeft; 
labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20); 

[labelSection setBackgroundColor:[UIColor clearColor]]; 
[labelSection setFont:[UIFont boldSystemFontOfSize:15]]; 
NSString *name = @"section title"; 
labelSection.text = name; 
[viewSection setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName"]]]; 
[labelSection setTextColor:[UIColor whiteColor]]; 
[viewSection addSubview:labelSection]; 
return viewSection; 
} 
1

Vous pouvez utiliser UITableViewHeaderFooterView. Créez votre vue personnalisée comme sous-classe de UITableViewHeaderFooterView. Utilisez les contraintes appropriées. Utilisez maintenant cette vue comme vue d'en-tête.

YourHeaderView *YourHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YourHeaderViewIdentifier"]; 
1

Essayez d'utiliser autolayout

[header setTranslatesAutoresizingMaskIntoConstraints:NO]; 
NSDictionary *views = NSDictionaryOfVariableBindings(header); 
NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[header]-|" options:0 metrics:nil views:views]; 
NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[header]-|" options:0 metrics:nil views:views]; 
[sectionView addConstraints:horizontalConstraints]; 
[sectionView addConstraints: verticalConstraints]; 

et votre code ressemblera ci-dessous

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

     UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, _TableList.frame.size.width, 80)]; 
     [header setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     header = [[HeaderView alloc] init]; 
     [sectionView addSubview:header]; 

     NSDictionary *views = NSDictionaryOfVariableBindings(header); 
     NSArray *horizontalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[header]-|" options:0 metrics:nil views:views]; 
     NSArray *verticalConstraints =[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[header]-|" options:0 metrics:nil views:views]; 

     [sectionView addConstraints:horizontalConstraints]; 
     [sectionView addConstraints: verticalConstraints]; 

     sectionView.tag=section; 
     sectionView.backgroundColor = [UIColor grayColor]; 

     UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)]; 
     [sectionView addGestureRecognizer:headerTapped]; 

     return sectionView; 
    } 
+0

merci de répondre, j'ai appliqué ce code mais le même problème à venir. –

+0

Quel problème vient? – iSashok

+0

même vue personnalisée n'est pas pour en-tête comme je l'ai mentionné ci-dessus –

0

Si vous utilisez le story-board puis créer une vue comme une cellule tableview

  1. Faites glisser et déposez 2 cellules de table
  2. Créez une cellule en tant que vue d'en-tête et une autre cellule utilisera comme cellule de liste de vues de table. Et donner la mise en page automatique des sous-vues de chaque cellule.

enter image description here

maintenant donner identifiant de cellule différent à chaque cellule de l'interface comme ci-dessous

enter image description here

utiliser maintenant cette cellule en vue de la section en vue d'en-tête délégué de tableview.

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static NSString *identifier = @"SectionHeader"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    return cell; 
} 

Espoir Ça aide!