这是我在cellForRowAtIndexPath委托中的代码:
Status *status = nil;
status = [myArray objectAtIndex:indexPath.row];
NSString* postvalue = status.statusContent;
cell.postContent.scrollEnabled = NO;
CGSize mysize = [postvalue sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]}];
CGRect frame = cell.postContent.frame;
frame.size.height = mysize.height;
cell.postContent.frame = frame;
cell.postContent.text = postvalue;
其中cell.postContent是textView
这是我调整单元格大小的代码:
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
Status *status = nil;
status = [myArray objectAtIndex:indexPath.row];
NSString* postvalue = status.statusContent;
return 100 + [self heightForText:postvalue];
}
-(CGFloat)heightForText:(NSString *)text
{
NSInteger MAX_HEIGHT = 2000;
UITextView * textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 226, MAX_HEIGHT)];
textView.text = text;
textView.font = [UIFont systemFontOfSize:17.0f];// your font
[textView sizeToFit];
return textView.frame.size.height;
}
这就是它的结果:
我已经用在cellForRow ..委托中计算的高度替换了用户名,这样你就可以看到正确计算高度,并且单元格正在正确调整大小,但是textview不是 - 还有更多的行比你看不到的。有什么想法吗?
由于
答案 0 :(得分:0)
那是因为你不应该创建新的textField(你也忘了添加为子视图),但要调整单元格的出口大小。尝试在创建单元格时在cellForRow中设置textField的高度
答案 1 :(得分:0)
UITextView
会在它周围采用一些默认填充,因此这可能是个问题。按代码设置contentInset
的{{1}}。
UITextView
答案 2 :(得分:0)
你可以访问它的内容视图我想,这是代码, 我正在使用一些值来测试它工作正常,通过这个希望帮助你... :)
禁用textfield的自动布局
在CustomCelll.h文件中
@interface CustomCelll : UITableViewCell
//i think u hav an outlet for textview
@property (retain, nonatomic) IBOutlet UITextView *postContent;
@property (nonatomic,retain)NSString *message;//you get the message to set the height
in CustomCelll.m file
@implementation CustomCelll
@synthesize postContent;
@synthesize imageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//heare i am adding the button
UIFont *font = [UIFont systemFontOfSize:17.0f];
self.postContent.frame = CGRectZero;//initially set it zero
self.postContent.font = font;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGSize size = [self findMessgeStringHeight];//you can ge the size
//set frame for ur label
self.postContent.frame = CGRectMake(103, 8, size.width + 10,size.height + 10);//set the height here(for your case) and disable auto layout
self.postContent.text = self.message;//set the message
}
- (CGSize)findMessgeStringHeight
{
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:self.message attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:17.0f] }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize requiredSize = rect.size;
return requiredSize; //finally u return your height
}
//in your controller
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCelll *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"Empty" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.message = @"hello, how are u, it been so long since we met, how's the weather there, here too cold, i am going to my mom's home may be i will stay there for 2 or 3 weeks";
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = @"hello, how are u, it been so long since we met, how's the weather there, here too cold, i am going to my mom's home may be i will stay there for 2 or 3 weeks";//use same sring to get the height
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:str attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:17.0f]
}];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize requiredSize = rect.size;
return requiredSize.height + 30;
}