le changement dynamique de la hauteur des cellules en objective-C
J'ai créé une application avec une vue de la table. Qui utilise un affichage et une étiquette dans chaque cellule. Mais si je créer les vues et les cellules (!la cellule) code il renvoie les cellules vides et si je supprime le (!la cellule), il affiche les données mais ne prend pas la dynamique de la hauteur. Quelqu'un peut s'il vous plaît aider moi.
- (void)viewDidLoad{
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", LanguageFile]];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:DataPath];
self.reloadArray = [tempDict objectForKey:@"Rows"];}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.reloadArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//Get data for the current row
NSString *textData = [reloadArray objectAtIndex:indexPath.section]
CGFloat dataTextHeight = [self getLabelHeightForIndex:textData];
if(dataTextHeight < 44)
{
dataTextHeight = 44;
}
return dataTextHeight;
}
-(CGFloat)getLabelHeightForIndex:(NSString *)string
{
CGSize maximumSize = CGSizeMake(280, 10000);
CGSize labelHeightSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
if(labelHeightSize.height < 44){
labelHeightSize.height = 44;
}
return labelHeightSize.height;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
static const int textViewTag = 1, textLabelTag = 2;
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"standard_back.png"]];
img.frame = tableView.frame;
tableView.backgroundView = img;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//First view
UIView *textView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 280.0, 36.00)];
textView.tag = textViewTag;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:textView];
//First label
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, 270.0, 36.00)];
textLabel.tag = textLabelTag;
textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0f];
textLabel.textColor = [UIColor whiteColor];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.numberOfLines = 0;
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//textLabel.clipsToBounds = YES;
[cell.contentView addSubview:textLabel];
}
NSString *textData = [reloadArray objectAtIndex:(indexPath.section)];
CGFloat dataTextHeight = [self getLabelHeightForIndex:textData];
UIView *textView = [cell.contentView viewWithTag:textViewTag];
CGRect textViewFrame = textView.frame;
textView.frame = CGRectMake(0.0, 0.0, textViewFrame.size.width, dataTextHeight);
UILabel *textLabel = [cell.contentView viewWithTag:textLabelTag];
CGRect textLabelFrame = textLabel.frame;
textLabel.frame = CGRectMake(10.0, 0.0, textLabelFrame.size.width, dataTextHeight);
textLabel.text = textData;
textLabel.backgroundColor= [UIColor clearColor];
textLabel.textAlignment = NSTextAlignmentCenter;
cell.backgroundColor = [UIColor colorWithWhite:0 alpha:.65];
cell.textLabel.numberOfLines = 0; //Multiline
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
return cell;
}
Merci d'avance.
cellule.cadre.taille.hauteur
double possible de Changement de UITableviewCell hauteur de façon dynamique dans l'iPhone?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
double possible de Changement de UITableviewCell hauteur de façon dynamique dans l'iPhone?
OriginalL'auteur Neha Dangui | 2013-03-25
Vous devez vous connecter pour publier un commentaire.
C'est une partie de mon code que j'ai utilisé dans mon application. Il fonctionne pour moi amende.Ping-moi si u besoin d'aide.
Espère que cela aide.!!!
Il suffit de supprimer tous les presse/conserver/autorelease appels.
r u à l'aide de l'ARC dans le projet d'ur?
il ne fonctionne pas sous iOS 7. s'il vous plaît aider.
OriginalL'auteur Dany
J'ai vu beaucoup de solutions, mais tout était faux ou uncomplet.
Vous pouvez résoudre tous les problèmes avec 5 lignes dans le viewDidLoad et de mise en page automatique.
Cette pour objectif C:
Pour swift 2.0:
Maintenant créer votre cellule avec xib ou en tableview dans votre Storyboard
Avec cela, vous pas besoin de mettre en œuvre rien de plus ou de remplacement. (N'oubliez pas le numéro de os les lignes 0) et le fond de l'étiquette (contraindre) downgrade "Contenu Étreindre Priorité-à la Verticale à 250"
Vous pouvez télécharger le code dans l'url suivant:
https://github.com/jposes22/exampleTableCellCustomHeight
Références: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
OriginalL'auteur Jose Pose S
OriginalL'auteur P.J.Radadiya