我是ios开发的新手,到目前为止这是我的问题。我能够通过委托“heightForRowAtIndexPath”动态计算自定义单元格的高度。所以在第一次加载时,每件事都显示得很好。
我的问题是,一旦我开始滚动,一切都会搞砸。我认为滚动“heightForRowAtIndexPath”时不再调用每个单元格来显示在屏幕上,因此无法计算新的单元格高度。
所以我已经被困在这里一段时间了。我想知道你们中是否有人可以伸出援助之手。提前谢谢你。
我会将代码发布到相关文件中。这些文件包括自定义单元格h和m文件。以及视图控制器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;
}
答案 0 :(得分:4)
如果单元格中的文本要更改,则需要在tableView上调用 reloadData ,否则 tableView:heightForRowAtIndexPath :won'被称为。
tableView的工作方式是它在每次重新加载(以及重新加载变量)时收集所有行的高度。这就是iOS知道整个表的高度的方式。在抓取单个单元格时,它不再调用 tableView:heightForRowAtIndexPath :通常调用 reloadData 非常快,此方法与其变体不同,不会导致任何滚动。
有关有用的背景,请参阅stackoverflow question and answer。
如果您需要做很多工作来计算高度并且您有数百或数千行,则会出现问题。在一个用例我喜欢这个,我缓存行高计算以获得不错的性能。但如果那不是你的情况,那就用 reloadData 做一点自由。
答案 1 :(得分:3)
有一个很好的教程可引导您完成确定适合文本所需高度和适当调整单元格高度所需的高度。
我自己没有尝试过教程中的方法,但看起来非常彻底。
希望这有帮助!
答案 2 :(得分:1)
您的tableView:heightForRowAtIndexPath:
似乎是正确的,但您可能需要检查以下有关XIB文件中labelDescription单元格标签的信息:
tableView:heightForRowAtIndexPath:
方法(在您的情况下为320)tableView:heightForRowAtIndexPath:
方法中使用的字体和大小匹配(在您的情况下,“Arial”大小为15)如果您怀疑在滚动后没有调用tableView:heightForRowAtIndexPath:
方法,则可以通过在该方法中添加临时NSLog
语句来轻松验证。如果它永远不会被调用,您可能忘记将视图控制器设置为UITableView
的{{1}}代理。