摘要
考虑到我们并不总是知道单元格的框架或其内容视图将会是什么样子(由于编辑、旋转、辅助视图等),当单元格包含可变高度的文本字段或标签时,在tableView:heightForRowAtIndexPath:
中计算高度的最佳方法是什么?
我的一个UITableViewController
包含以下演示:
UITableViewCell带有UITextView。
UITextView应该与UITableViewCell的宽度和高度相同。
我创建了UITableViewCell子类,然后用UITextView进行初始化(UITextView是我的UITableViewController的私有字段)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"TextViewCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[BTExpandableTextViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier textView:_notesTextView] autorelease];
}
return cell;
}
我在我的UITableViewCell子类中实现了以下方法:
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat height = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width, MAXFLOAT)].height + textView.font.lineHeight;
textView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, (height < textView.font.lineHeight * 4) ? textView.font.lineHeight * 4 : height);
[self.contentView addSubview:textView];
}
当然,我实现了以下的UITableViewDataSource方法(看!我使用self.view.frame.size.width(但实际上我需要UITableViewCell contentView frame的宽度):
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
CGFloat height = [_notesTextView.text sizeWithFont:_notesTextView.font
constrainedToSize:CGSizeMake(self.view.frame.size.width, MAXFLOAT)].height;
CGFloat groupedCellCap = 20.0;
height += groupedCellCap;
if(height < [BTExpandableTextViewCell minimumTextViewHeightWithFont:_notesTextView.font]){
height = [BTExpandableTextViewCell minimumTextViewHeightWithFont:_notesTextView.font];
}
return height;
}
我还实现了以下方法(虽然不是那么重要,但我还是会发布它,只是为了解释一下单元格的高度是动态的,当更改UITextView中的文本后,它会收缩或展开)
- (void)textViewDidChange:(UITextView *)textView{
CGFloat height = [_notesTextView.text sizeWithFont:_notesTextView.font
constrainedToSize:CGSizeMake(_notesTextView.frame.size.width, MAXFLOAT)].height;
if(height > _notesTextView.frame.size.height){
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
}
现在,我的问题是: 加载视图后,UITableViewController会按以下顺序调用方法:(为简化起见,我将删除一些,如titleForHeaderInSection等)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
并且仅限于这样
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
看,我应该在cellForRowAtIndexPath之前返回正确的UITableViewCell高度!这意味着:我不知道UITableViewCell contentView的框架。并且我不能通过编程方式获取它。
这个宽度可以是以下之一:
1. iPhone普通表格,纵向方向 2. iPhone普通表格,横向方向 3. iPhone分组表格,纵向方向 4. iPhone分组表格,横向方向 5. iPad也是如此(另外4个值)
而且不要忘记,由于UITableViewCell accessoryType或UITableView编辑状态,contentView frame可能会更小。(例如,如果我们有带有任何高度的多行UILabel和任何accessoryView的UITableViewCell,并且处于任何编辑状态)。
所以这个问题是根本性的:我只是无法获得用于限制的单元格contentView框架宽度,因为我应该在单元格布局contentView之前返回此高度。(顺便说一下,这很合理)但是这个contentView框架真的很重要。
当然,有时我可以确切地知道这个宽度并“硬编码”它(例如:UITableViewCellAccessoryDisclosureIndicator的宽度为20像素,tableView不能处于编辑状态,那么我可以写self.view.frame.size.width - 20,任务就完成了)!或者有时contentView等于UITableViewController的视图框架!
有时我在-tableView: heightForRowAtIndexPath: 方法中使用self.view.frame.width。(像现在这样,它工作得很好,但是由于分组UITableView的缘故,不完美应该减去一些常数值,它们对于2个设备* 2个方向是不同的)。
有时我在UITableViewCell中使用一些#defined常量(如果我确切地知道宽度)...
有时我使用一些虚拟预分配的UITableViewCell(这只是愚蠢的,但有时非常优雅且易于使用)...
但我不喜欢其中任何一个。
最好的决定是什么? 也许我应该创建一些帮助类,该类将使用以下参数进行初始化: accessory views、设备方向、设备类型、表视图编辑状态、表视图样式(普通、分组)、控制器视图框架和其他一些包括一些常量的内容(例如分组tableView偏移量等),并用它来查找预期的UITableViewCell contentView宽度? ;)
谢谢