调整UITextView和UITableViewCell的大小

时间:2013-12-22 10:43:33

标签: objective-c uitableview ios7 uitextview xcode5

我有新的客观C编程,我目前正在尝试开发iOS应用程序。我正在将评论从服务器加载到UITextView中的UITableViewCell

这是用于将数据加载到UITextview中的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath {
  // set the location to read the sample data
  static NSString *CellIdentifier = @"CommentCell";
  CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

PostModel *posts;
CommentModel *comments;
if (contentTitle == nil) {
    posts = _feed.posts[articleIndexPath.section];
    comments = posts.comments[indexPath.section];
}
else {
    posts = _feed.posts[searchResult];
    comments = posts.comments[indexPath.section];
}

NSString *content = comments.content;
content = [content stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
content = [content stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
cell.usernameLabel.text = comments.name;
[cell.commentTextView setScrollEnabled:YES];
cell.commentTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.commentTextView.text = content;
[cell.commentTextView sizeToFit];
[cell.commentTextView setScrollEnabled:NO];
[cell.commentTextView setTextColor:[UIColor whiteColor]];

[cell.usernameLabel setTextColor:[UIColor whiteColor]];

// make the borders of the cell round
[cell.layer setCornerRadius:7.0f];
[cell.layer setMasksToBounds:YES];
cell.contentView.layer.borderColor = [[UIColor colorWithRed:51/255 green:56/255 blue:67/255 alpha:1] CGColor];

//make the borders of the image round
[cell.userImage.layer setMasksToBounds:YES];
[cell.userImage.layer setCornerRadius:7.0f];

return cell;
}

这是我用来调整UITableViewCellUITextView

大小的代码
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CommentCell";
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PostModel *posts;
CommentModel *comments;
if (contentTitle == nil) {
    posts = _feed.posts[articleIndexPath.section];
    comments = posts.comments[indexPath.section];
}
else {
    posts = _feed.posts[searchResult];
    comments = posts.comments[indexPath.section];
};

if (cell.commentTextView.textContainer.size.height >= 40) {
    float height = [self heightForTextView:cell.commentTextView containingString:comments.content];
    return height;
}
else {
    return 79;
   }
}

这是我用来检测文本高度的方法

- (CGFloat)heightForTextView:(UITextView *)textView containingString:(NSString *)string {
float horizontalPadding = 8;
float verticalPadding = 16;

float widthOfTextView = textView.textContainer.size.width - horizontalPadding;
float height = [string sizeWithFont:textView.font constrainedToSize:CGSizeMake(widthOfTextView, 999999.0f) lineBreakMode:NSLineBreakByWordWrapping].height + verticalPadding;

return height;
}

- (CGSize)text: (NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    CGRect frame = [text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributesDictionary context:nil];

    return frame.size;
}
else {
    return [text sizeWithFont:font constrainedToSize:size];
   }
}

我最初运行它时加载正常,但当我向下滚动并向后滚动时,UITextView成为垂直单行列。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我认为这可能是原因:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

您正在使用dequeued单元格的文本视图来确定单元格的高度。但是,dequeued单元格中可能没有任何文字数据(或不同的文字),加上commentViewUIViewAutoresizingFlexibleWidth,宽度由

计算
float widthOfTextView = textView.textContainer.size.width - horizontalPadding;
在向下滚动和备份的情况下,

可能非常小。