Le réglage personnalisé UITableViewCells hauteur

voix
209

J'utilise une UITableViewCell personnalisée qui a des étiquettes, des boutons et des vues d'image à afficher. Il y a une étiquette dans la cellule dont le texte est un NSStringobjet et la longueur de la chaîne pourrait être variable. En raison de cela, je ne peux pas définir une hauteur constante à la cellule dans le UITableView« s heightForCellAtIndexméthode. La hauteur dépend de la hauteur de l'étiquette de la cellule qui peut être déterminée en utilisant le NSString« s sizeWithFontméthode. J'ai essayé de l' utiliser, mais il semble que je vais mal quelque part. Comment peut - il être fixé?

Voici le code utilisé pour initialiser la cellule.

if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImage *image = [UIImage imageNamed:@dot.png];
    imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(45.0,10.0,10,10);

    headingTxt = [[UILabel alloc] initWithFrame:   CGRectMake(60.0,0.0,150.0,post_hdg_ht)];
    [headingTxt setContentMode: UIViewContentModeCenter];
    headingTxt.text = postData.user_f_name;
    headingTxt.font = [UIFont boldSystemFontOfSize:13];
    headingTxt.textAlignment = UITextAlignmentLeft;
    headingTxt.textColor = [UIColor blackColor];

    dateTxt = [[UILabel alloc] initWithFrame:CGRectMake(55.0,23.0,150.0,post_date_ht)];
    dateTxt.text = postData.created_dtm;
    dateTxt.font = [UIFont italicSystemFontOfSize:11];
    dateTxt.textAlignment = UITextAlignmentLeft;
    dateTxt.textColor = [UIColor grayColor];

    NSString * text1 = postData.post_body;
    NSLog(@text length = %d,[text1 length]);
    CGRect bounds = [UIScreen mainScreen].bounds;
    CGFloat tableViewWidth;
    CGFloat width = 0;
    tableViewWidth = bounds.size.width/2;
    width = tableViewWidth - 40; //fudge factor
    //CGSize textSize = {width, 20000.0f}; //width and height of text area
    CGSize textSize = {245.0, 20000.0f}; //width and height of text area
    CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:11.0f]
                        constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

    CGFloat ht = MAX(size1.height, 28);
    textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)];
    textView.text = postData.post_body;
    textView.font = [UIFont systemFontOfSize:11];
    textView.textAlignment = UITextAlignmentLeft;
    textView.textColor = [UIColor blackColor];
    textView.lineBreakMode = UILineBreakModeWordWrap;
    textView.numberOfLines = 3;
    textView.autoresizesSubviews = YES;

    [self.contentView addSubview:imageView];
    [self.contentView addSubview:textView];
    [self.contentView addSubview:webView];
    [self.contentView addSubview:dateTxt];
    [self.contentView addSubview:headingTxt];
    [self.contentView sizeToFit];

    [imageView release];
    [textView release];
    [webView release];
    [dateTxt release];
    [headingTxt release];
}

Ceci est l'étiquette dont la hauteur et la largeur va mal:

textView = [[UILabel alloc] initWithFrame:CGRectMake(55.0,42.0,245.0,ht)];
Créé 30/01/2009 à 06:27
source utilisateur
Dans d'autres langues...                            


9 réponses

voix
483

Votre UITableViewDelegatedevrait mettre en œuvretableView:heightForRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [indexPath row] * 20;
}

Vous voudrez probablement utiliser NSStringde » sizeWithFont:constrainedToSize:lineBreakMode:méthode pour calculer la hauteur de votre ligne plutôt que d' effectuer quelques calculs stupides sur le indexPath :)

Créé 30/01/2009 à 23:46
source utilisateur

voix
121

Si toutes vos lignes sont à la même hauteur, situé juste à la rowHeightpropriété du lieu UITableView que la mise en œuvre du heightForRowAtIndexPath. Apple Docs:

Il y a des implications de performance à l'aide tableView: heightForRowAtIndexPath: au lieu de rowHeight. Chaque fois une vue de tableau est affiché, il appelle tableView: heightForRowAtIndexPath: le délégué pour chacune de ses lignes, ce qui peut entraîner un problème de performance significative avec des vues de table ayant un grand nombre de lignes (environ 1000 ou plus).

Créé 19/01/2011 à 00:59
source utilisateur

voix
22

dans une UITableViewCell mesure -Controller ajouter cette

-(void)layoutSubviews {  

    CGRect newCellSubViewsFrame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    CGRect newCellViewFrame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);

    self.contentView.frame = self.contentView.bounds = self.backgroundView.frame = self.accessoryView.frame = newCellSubViewsFrame;
    self.frame = newCellViewFrame;

    [super layoutSubviews];
}

Dans le UITableView -Controller ajouter cette

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [indexPath row] * 1.5; // your dynamic height...
}
Créé 19/01/2010 à 14:26
source utilisateur

voix
17
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 300.0f
#define CELL_CONTENT_MARGIN 10.0f

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath      *)indexPath;
{
   /// Here you can set also height according to your section and row
   if(indexPath.section==0 && indexPath.row==0)
   {
     text=@"pass here your dynamic data";

     CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

     CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE]      constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

     CGFloat height = MAX(size.height, 44.0f);

     return height + (CELL_CONTENT_MARGIN * 2);
   }
   else
   {
      return 44;
   }
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    UILabel *label = nil;

    cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
    {
       cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"];
    }
    ********Here you can set also height according to your section and row*********
    if(indexPath.section==0 && indexPath.row==0)
    {
        label = [[UILabel alloc] initWithFrame:CGRectZero];
        [label setLineBreakMode:UILineBreakModeWordWrap];
        [label setMinimumFontSize:FONT_SIZE];
        [label setNumberOfLines:0];
        label.backgroundColor=[UIColor clearColor];
        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [label setTag:1];

        // NSString *text1 =[NSString stringWithFormat:@"%@",text];

        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

        if (!label)
        label = (UILabel*)[cell viewWithTag:1];


        label.text=[NSString stringWithFormat:@"%@",text];
        [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH          - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
        [cell.contentView addSubview:label];
    }
return cell;
}
Créé 19/09/2012 à 10:30
source utilisateur

voix
8
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    CGSize constraintSize = {245.0, 20000}
    CGSize neededSize = [ yourText sizeWithFont:[UIfont systemFontOfSize:14.0f] constrainedToSize:constraintSize  lineBreakMode:UILineBreakModeCharacterWrap]
if ( neededSize.height <= 18) 

   return 45
else return neededSize.height + 45 
//18 is the size of your text with the requested font (systemFontOfSize 14). if you change fonts you have a different number to use  
// 45 is what is required to have a nice cell as the neededSize.height is the "text"'s height only
//not the cell.

}
Créé 18/11/2012 à 17:20
source utilisateur

voix
5

J'ai vu beaucoup de solutions mais tous eu tort ou uncomplet. Vous pouvez résoudre tous les problèmes avec 5 lignes viewDidLoad et autolayout. Ceci pour objetif C:

_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;

Pour 2.0 rapide:

 self.tableView.estimatedRowHeight = 80
 self.tableView.rowHeight = UITableViewAutomaticDimension      
 self.tableView.setNeedsLayout()
 self.tableView.layoutIfNeeded()
 self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

Maintenant, créez votre cellule avec ou dans xib tableview dans votre Storyboard Avec cela, vous pas besoin de mettre en œuvre ni plus ni passer outre. (Don oublier nombre de lignes os 0) et l'étiquette de fond (limiter) déclasser « Contenu étreignant Priorité - verticale à 250 »

entrez la description d'image ici entrez la description d'image ici

Vous pouvez donwload le code dans l'url suivante: https://github.com/jposes22/exampleTableCellCustomHeight

Les références: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

Créé 26/01/2016 à 19:58
source utilisateur

voix
5

Merci à tous les messages sur ce sujet, il existe des moyens très utiles pour régler la rowHeight d'un UITableViewCell.

Voici une compilation de quelques-uns des concepts de tout le monde qui aide vraiment lors de la construction pour l'iPhone et l'iPad. Vous pouvez également accéder à des sections différentes et les ajuster en fonction des différentes tailles de vues.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    int cellHeight = 0;

    if ([indexPath section] == 0) 
    {
        cellHeight = 16;
        settingsTable.rowHeight = cellHeight;
    }
    else if ([indexPath section] == 1)
    {
        cellHeight = 20;
        settingsTable.rowHeight = cellHeight;
    }

    return cellHeight;
}
else
{
    int cellHeight = 0;

    if ([indexPath section] == 0) 
    {
        cellHeight = 24;
        settingsTable.rowHeight = cellHeight;
    }
    else if ([indexPath section] == 1)
    {
        cellHeight = 40;
        settingsTable.rowHeight = cellHeight;
    }

    return cellHeight;
}
return 0;
} 
Créé 22/03/2012 à 21:02
source utilisateur

voix
3

Pour avoir la hauteur de la cellule dynamique que le texte de l' étiquette augmente, vous devez d'abord calculer la hauteur, que le texte va être utilisé dans la -heightForRowAtIndexPathméthode de délégué et de le retourner avec les hauteurs ajoutées d'autres lables, images (hauteur max de texte + hauteur d'autres statique composant graphique ) et utiliser même hauteur dans la création de cellules.

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 300.0f  
#define CELL_CONTENT_MARGIN 10.0f

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{

    if (indexPath.row == 2) {  // the cell you want to be dynamic

        NSString *text = dynamic text for your label;

        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

        CGFloat height = MAX(size.height, 44.0f);

        return height + (CELL_CONTENT_MARGIN * 2);
    }
    else {
        return 44; // return normal cell height
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UILabel *label;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
    }

    label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 34)];

    [label setNumberOfLines:2];

    label.backgroundColor = [UIColor clearColor];

    [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];

    label.adjustsFontSizeToFitWidth = NO;

    [[cell contentView] addSubview:label];


    NSString *text = dynamic text fro your label;

    [label setText:text];

    if (indexPath.row == 2) {// the cell which needs to be dynamic 

        [label setNumberOfLines:0];

        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

        [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

    }
    return  cell;
}
Créé 01/08/2012 à 12:07
source utilisateur

voix
2

Pour définir la dimension automatique de la hauteur des lignes et la hauteur de ligne estimée, assurez-vous les étapes suivantes pour faire, la dimension automatique efficace pour la mise en page de hauteur cellules / rangée.

  • Assigner et mettre en œuvre dataSource tableview et déléguer
  • Assigner UITableViewAutomaticDimensionà rowHeight & estimatedRowHeight
  • Mettre en œuvre délégué / méthodes de source de données ( par exemple heightForRowAtet retourner une valeur UITableViewAutomaticDimensionà elle)

-

Objectif c:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

Rapide:

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Par exemple d'étiquette UITableViewCell

  • Définir le nombre de lignes = 0 (et la ligne de mode de coupure = tronquer queue)
  • Régler toutes les contraintes (en haut, en bas, à gauche à droite) par rapport à son conteneur superview / cellule.
  • En option : Régler la hauteur minimum pour l' étiquette, si vous voulez zone verticale minimale couverte par l' étiquette, même s'il n'y a pas de données.

entrez la description d'image ici

Remarque : Si vous avez plus d'un (étiquettes UIElements) dont la longueur dynamique, qui doit être ajustée en fonction de la taille du contenu: Régler « Contenu étreintes et Résistance à la compression priority` pour les étiquettes que vous souhaitez développer / compresser avec une priorité plus élevée.

Ici, dans cet exemple, je mets à faible priorité étreintes et de haute résistance à la compression, qui conduit à définir une plus grande priorité / importance pour le contenu de la deuxième étiquette (jaune).

entrez la description d'image ici

Créé 02/02/2018 à 15:20
source utilisateur

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more