Я новичок в разработке ios, и это моя проблема до сих пор. Я могу динамически рассчитать высоту пользовательской ячейки с помощью делегата "heightForRowAtIndexPath". Итак, при первой загрузке все отображается отлично.
Моя проблема в том, что как только я начинаю прокручивать, все просто портится. Я думаю, что при прокрутке «heightForRowAtIndexPath» больше не вызывается для каждой ячейки, отображаемой на экране, поэтому высоту новой ячейки вычислить невозможно.
Так что я застрял здесь на некоторое время. Мне было интересно, если кто-нибудь из вас может помочь мне. заранее спасибо.
Я выложу код в соответствующие файлы. Эти файлы включают в себя пользовательские ячейки h и m 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;
}