Iphone - when to calculate heightForRowAtIndexPath for a tableview when each cell height is dynamic?

前端 未结 10 1296
迷失自我
迷失自我 2020-11-30 19:50

I have seen this question asked many times but astoundingly, I have not seen a consistent answer, so I will give it a try myself:

If you have a tableview containing

10条回答
  •  既然无缘
    2020-11-30 20:16

    I went with the idea I originally proposed, which appears to work fine, whereby I load all the custom cells ahead of time in viewDidLoad, store them in a NSMutableDictionary with their index as the key. I am posting the relevant code and would love any critiques or opinions anyone has about this approach. Specifically, I am not sure whether there is any memory leak issue with the way I am creating the UITableViewCells from the nib in viewDidLoad - since I don't release them.

    @interface RecentController : UIViewController  {
    
    NSArray *listData;
    NSMutableDictionary *cellBank;
    
    }
    
    @property (nonatomic, retain) NSArray *listData;
    @property (nonatomic, retain) NSMutableDictionary *cellBank;
    @end
    
    
    
    @implementation RecentController
    
    @synthesize listData;
    @synthesize cellBank;
    
    ---
    
    - (void)viewDidLoad {
    
    ---
    
    self.cellBank = [[NSMutableDictionary alloc] init];
    
    ---
    
    //create question objects…
    
    --- 
    
    NSArray *array = [[NSArray alloc] initWithObjects:question1,question2,question3, nil];
    
    self.listData = array;
    
    //Pre load all table row cells
    int count = 0;
    for (id question in self.listData) {
    
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QuestionHeaderCell" 
                                                     owner:self 
                                                   options:nil];
        QuestionHeaderCell *cell;
    
        for (id oneObject in nib) {
            if([oneObject isKindOfClass:[QuestionHeaderCell class]])
                cell = (QuestionHeaderCell *) oneObject;
    
                NSNumber *key = [NSNumber numberWithInt:count];
                [cellBank setObject:[QuestionHeaderCell makeCell:cell 
                                                      fromObject:question] 
                             forKey:key];
                count++;
    
        }
    }
    
    [array release];
    [super viewDidLoad];
    }
    
    
    
    #pragma mark -
    #pragma mark Table View Data Source Methods
    
    -(NSInteger) tableView: (UITableView *) tableView
    numberOfRowsInSection: (NSInteger) section{
    
    return [self.listData count];
    
    }
    
    -(UITableViewCell *) tableView: (UITableView *) tableView
         cellForRowAtIndexPath: (NSIndexPath *) indexPath{
    
    NSNumber *key = [NSNumber numberWithInt:indexPath.row];
    return [cellBank objectForKey:key];
    
    
    }
    
    -(CGFloat) tableView: (UITableView *) tableView
    heightForRowAtIndexPath: (NSIndexPath *) indexPath{
    
    NSNumber *key = [NSNumber numberWithInt:indexPath.row];
    return [[cellBank objectForKey:key] totalCellHeight];
    
    }
    
    @end
    
    
    
    @interface QuestionHeaderCell : UITableViewCell {
    
    UITextView *title;
    UILabel *createdBy;
    UILabel *category;
    UILabel *questionText;
    UILabel *givenBy;
    UILabel *date;
    int totalCellHeight;
    
    }
    
    @property (nonatomic, retain) IBOutlet UITextView *title;
    @property (nonatomic, retain) IBOutlet UILabel *category;
    @property (nonatomic, retain) IBOutlet UILabel *questionText;
    @property (nonatomic, retain) IBOutlet UILabel *createdBy;
    @property (nonatomic, retain) IBOutlet UILabel *givenBy;
    @property (nonatomic, retain) IBOutlet UILabel *date;
    @property int totalCellHeight;
    
    +(UITableViewCell *) makeCell:(QuestionHeaderCell *) cell 
                   fromObject:(Question *) question;
    
    @end
    
    
    
    @implementation QuestionHeaderCell
    @synthesize title;
    @synthesize createdBy;
    @synthesize givenBy;
    @synthesize questionText;
    @synthesize date;
    @synthesize category;
    @synthesize totalCellHeight;
    
    
    
    
    
    
    
    - (void)dealloc {
    [title release];
    [createdBy release];
    [givenBy release];
    [category release];
    [date release];
    [questionText release];
    [super dealloc];
    }
    
    +(UITableViewCell *) makeCell:(QuestionHeaderCell *) cell 
                     fromObject:(Question *) question{
    
    
    NSUInteger currentYpos = 0;
    
    cell.title.text = question.title;
    
    CGRect frame = cell.title.frame;
    frame.size.height = cell.title.contentSize.height;
    cell.title.frame = frame;
    currentYpos += cell.title.frame.size.height + 2;
    
    
    NSMutableString *tempString = [[NSMutableString alloc] initWithString:question.categoryName];
    [tempString appendString:@"/"];
    [tempString appendString:question.subCategoryName];
    
    cell.category.text = tempString;
    frame = cell.category.frame;
    frame.origin.y = currentYpos;
    cell.category.frame = frame;
    currentYpos += cell.category.frame.size.height;
    
    [tempString setString:@"Asked by "];
    [tempString appendString:question.username];
    cell.createdBy.text = tempString;
    
    frame = cell.createdBy.frame;
    frame.origin.y = currentYpos;
    cell.createdBy.frame = frame;
    currentYpos += cell.createdBy.frame.size.height;
    
    
    cell.questionText.text = question.text;
    frame = cell.questionText.frame;
    frame.origin.y = currentYpos;
    cell.questionText.frame = frame;
    currentYpos += cell.questionText.frame.size.height;
    
    
    [tempString setString:@"Advice by "];
    [tempString appendString:question.lastNexusUsername];
    cell.givenBy.text = tempString;
    frame = cell.givenBy.frame;
    frame.origin.y = currentYpos;
    cell.givenBy.frame = frame;
    currentYpos += cell.givenBy.frame.size.height;
    
    
    cell.date.text = [[[MortalDataStore sharedInstance] dateFormat] stringFromDate: question.lastOnDeck];
    frame = cell.date.frame;
    frame.origin.y = currentYpos-6;
    cell.date.frame = frame;
    currentYpos += cell.date.frame.size.height;
    
    //Set the total height of cell to be used in heightForRowAtIndexPath
    cell.totalCellHeight = currentYpos;
    
    [tempString release];
    return cell;
    
    }
    
    @end
    

提交回复
热议问题