IOS: dynamic height with custom uitableviewcell

I am new to ios development and still this is my problem. I can dynamically calculate the height of a custom cell through the delegate "heightForRowAtIndexPath". Thus, on the first boot, each item is displayed perfectly.

My problem is, as soon as I start scrolling, everything just goes bad. I think that when scrolling, "heightForRowAtIndexPath" is no longer called on the cell that displays, so the height of the new cell cannot be determined.

So, I'm stuck here for a while. I was wondering if any of you could lend a hand to me. thank you in advance.

I will send the code to the appropriate files. These files include user cells h and m. and corresponding functions of the view manager file m.

// ###################################################### // HelpTableViewCell.h #import <UIKit/UIKit.h> @interface HelpTableViewCell : UITableViewCell { IBOutlet UILabel *labelName; IBOutlet UILabel *labelDescription; IBOutlet UIView *viewBackground; } @property (nonatomic, retain) UILabel *labelName; @property (nonatomic, retain) UILabel *labelDescription; @property (nonatomic, retain) UIView *viewBackground; @end // ###################################################### // HelpTableViewCell.m #import "HelpTableViewCell.h" @implementation HelpTableViewCell @synthesize labelName; @synthesize labelDescription; @synthesize viewBackground; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { self.labelName.lineBreakMode = UILineBreakModeWordWrap; self.labelName.numberOfLines = 0; self.labelName.font = [UIFont boldSystemFontOfSize:15]; [self.labelName sizeToFit]; self.labelDescription.lineBreakMode = UILineBreakModeWordWrap; self.labelDescription.numberOfLines = 0; self.labelDescription.font = [UIFont fontWithName:@"Arial" size:15]; [self.labelDescription sizeToFit]; [super setSelected:selected animated:animated]; } - (void) dealloc { [labelName release], labelName = nil; [labelDescription release], labelDescription = nil; [super dealloc]; } @end // ###################################################### // in my view controller m file - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"HelpTableViewCell"; HelpTableViewCell *cell = (HelpTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HelpTableViewCell" owner:nil options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (HelpTableViewCell *) currentObject; break; } } } cell.labelName.text = [self.instructionName objectAtIndex:indexPath.row]; cell.labelDescription.text = [self.instructionDescription objectAtIndex:indexPath.row]; return cell; } - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellText = [self.instructionDescription objectAtIndex:indexPath.row]; UIFont *cellFont = [UIFont fontWithName:@"Arial" size:15]; CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT); CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; return labelSize.height + 25; } 
+9
ios objective-c iphone uitableview ios4
Aug 19 2018-11-11T00:
source share
3 answers

If the text in the cells changes, you will need to call reloadData strong> in your tableView, if that happens, otherwise tableView: heightForRowAtIndexPath : won't call.

How tableView works, it collects heights for all rows at every reload (and reload options). This is how iOS knows the height of the entire table. It does not call tableView: heightForRowAtIndexPath : again when capturing individual cells. Typically, the call to reloadData strong> is quite fast, and this method, unlike its variants, does not cause scrolling.

See https://stackoverflow.com>

Problems arise if you have to work hard to calculate heights, and you have hundreds or thousands of lines. In one case, I have this, I cache line height calculations to get decent performance. But if this is not your case, just be a little more liberal with reloadData strong>.

+4
Aug 20 '11 at 8:44
source share

There is a good tutorial in which you will learn how to determine the height needed for your text, and determine the cell size accordingly.

the tutorial is available here: http://www.raddonline.com/blogs/geek-journal/iphone-sdk-resizing-a-uitableviewcell- in hold-variable-quantity-in-text /

I myself have not tried to use the methods in the textbook, but it looks pretty solid.

Hope this helps!

+3
Aug 20 2018-11-11T00:
source share

Your tableView:heightForRowAtIndexPath: seems correct, but you can check the following about the label label labelDescription in your XIB file:

  • should match the width used in the tableView:heightForRowAtIndexPath: method (in your case 320)
  • should match the font and size used in the tableView:heightForRowAtIndexPath: method (in your case, the size is "Arial").
  • set the "Correct as appropriate" label to NO
  • set the value of # of lines to 0 (thus, it allows the text to span multiple lines)

If you suspect that the tableView:heightForRowAtIndexPath: method is not called after scrolling, you can easily make sure that you add a temporary NSLog statement to this method. If it will never be called, you may have forgotten to set your view controller as a UITableView UITableViewDelegate delegate.

+1
Aug 20 2018-11-11T00:
source share



All Articles