摘要
鉴于我们并不总是知道单元格的框架或其内容视图将是什么(由于编辑,旋转,附件视图等),tableView:heightForRowAtIndexPath:当单元格包含一个单元格时,计算高度的最佳方法是什么?可变高度文本字段或标签?
我的一个UITableViewController's包含以下演示文稿: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;
}
Run Code Online (Sandbox Code Playgroud)
我在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];
}
Run Code Online (Sandbox Code Playgroud)
当然我实现了以下UITableViewDataSource方法(看!我使用的是self.view.frame.size.width(但实际上我需要UITableViewCell contentView框架宽度):
- (CGFloat)tableView:(UITableView*)tableView …Run Code Online (Sandbox Code Playgroud) 嘿有经验的程序员!
我想问你一些干净的代码:)
我想保持我的代码干净,让它变得更好,所以这就是我的问题:
1)我应该把#imports放在哪里?我的原则:(我不认为它们很好)
代表们应该在.m
@interface ViewController() <UIAlertViewDelegate>
@end
Run Code Online (Sandbox Code Playgroud)2)我应该在哪里放置我的实例变量?
3)我应该把我的方法放在哪里?
(顺便说一句,这很明显)
4)#define?怎么样?
5)我应该在哪里放置NSNotification全局标识符以及如何组织它们
但......
一年前,我有另一种情况 - 一切都在.h,但我认为它也很糟糕
该怎么办?你使用什么原则?谢谢!
这是关于编码风格的问题,而不是关于"如何使其可编辑"的问题