IOS:与自定义的uitableviewcelldynamic高度

我是ios开发新手,这是我的问题。 我能够通过委托“heightForRowAtIndexPath”dynamic地计算自定义单元格的高度。 所以在第一次加载时,每件事情都显示得非常好。

我的问题是,只要我开始滚动,一切都只是搞砸了。 我认为,滚动“heightForRowAtIndexPath”不再被称为每个细胞,在屏幕上显示,所以新的细胞高度不能计算。

所以我在这里呆了一会儿 我想知道你们有没有人能帮我一把。 先谢谢你。

我会将代码发布到相关文件。 这些文件包括自定义单元格h和m文件。 以及视图控制器m文件的相关function。

// ###################################################### // 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; } 

如果单元格中的文本将发生变化,那么当您的tableView执行时,您将需要调用reloadData ,否则将不会调用tableView:heightForRowAtIndexPath:。

tableView的工作方式是在每次重新载入(和重新载入变体)时收集所有行的高度。 这就是iOS如何知道整个表的高度。 它不会调用tableView:heightForRowAtIndexPath :当抓取单个单元格时。 通常调用reloadData是相当快的,这种方法,不像其变种,不会导致任何滚动。

请参阅有用的背景的stackoverflow问题和答案 。

如果你必须做很多工作来计算高度,并且你有成百上千的行,就会出现问题。 在一个用例中,我有这样的情况,我caching行高计算,以获得体面的performance。 但是,如果这不是你的情况,只需要reloadData更自由一点。

有一个很好的教程,可以引导您确定适合您的文本所需的高度,并适当调整单元格高度。

教程可在这里find: http : //www.raddonline.com/blogs/geek-journal/iphone-sdk-resizing-a-uitableviewcell-to-hold-variable-amounts-of-text/

我自己并没有尝试教程中的方法,但看起来相当彻底。

希望这可以帮助!

你的tableView:heightForRowAtIndexPath:似乎是正确的,但是你可能想要检查下面关于你的XIB文件中的labelDescription单元格标签:

  • 必须匹配在tableView:heightForRowAtIndexPath:方法中使用的宽度(在你的情况下,320)
  • 必须与tableView:heightForRowAtIndexPath:方法中使用的字体和大小相匹配(在您的情况下,“Arial”大小为15)
  • 将标签的“调整为适合”checkbox设置为NO
  • 设置线的属性为0(这样,它允许文本跨越多行)

如果您怀疑tableView:heightForRowAtIndexPath:方法在滚动后未被调用,则可以通过在该方法中添加临时NSLog语句来轻松validation该方法。 如果它永远不会被调用,你可能忘了把你的视图控制器设置为UITableViewUITableViewDelegate委托。