Dan*_*yen 10 iphone objective-c uitableview ios4 ios
我是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;
}
Run Code Online (Sandbox Code Playgroud)
如果单元格中的文本要更改,则需要在 tableView 上调用reloadData,否则tableView:heightForRowAtIndexPath : 将不会被调用。
tableView 的工作方式是在每次重新加载(以及重新加载变体)时收集所有行的高度。这就是 iOS 知道整个桌子的高度的方式。当抓取单个单元格时,它不会再次调用tableView:heightForRowAtIndexPath : 。通常调用reloadData非常快,并且此方法与其变体不同,不会导致任何滚动。
有关有用的背景信息,请参阅stackoverflow 问题和答案。
如果您必须做大量工作来计算高度并且有数百或数千行,就会出现问题。在这样的一个用例中,我缓存行高计算以获得不错的性能。但如果这不是您的情况,请对reloadData更加自由一点。
| 归档时间: |
|
| 查看次数: |
20716 次 |
| 最近记录: |