我是 ios 开发的新手,这是我目前遇到的问题。我能够通过委托(delegate)“heightForRowAtIndexPath”动态计算自定义单元格的高度。所以在第一次加载时,一切都显示得很好。
我的问题是,一旦我开始滚动,一切都变得一团糟。我认为在滚动时不再为每个显示在屏幕上的单元格调用“heightForRowAtIndexPath”,因此无法计算新的单元格高度。
所以我被困在这里有一段时间了。我想知道你们中是否有人能帮我一把。提前谢谢你。
我会将代码发布到相关文件中。这些文件包括自定义单元 h 和 m 文件。以及view controller 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;
}
最佳答案
如果单元格中的文本要更改,您需要在 tableView 发生更改时调用 reloadData,否则 tableView:heightForRowAtIndexPath: 不会不叫。
tableView 的工作方式是在每次重新加载(和重新加载变体)时收集所有行的高度。 iOS 就是这样知道整个表格的高度的。它不会在抓取单个单元格时再次调用 tableView:heightForRowAtIndexPath: 。通常调用 reloadData 非常快,而且与其变体不同,此方法不会导致任何滚动。
参见 stackoverflow question and answer有用的背景。
如果您必须做很多工作来计算高度并且您有成百上千行,就会出现问题。在一个这样的用例中,我缓存了行高计算以获得不错的性能。但如果您不是这种情况,请对 reloadData 放宽一些。
关于iphone - IOS:带有自定义 uitableviewcell 的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7128215/