【问题标题】:Resizing UITextView and UITableViewCell调整 UITextView 和 UITableViewCell 的大小
【发布时间】:2014-01-10 18:50:29
【问题描述】:

我刚接触目标 C 编程,目前正在尝试开发 iOS 应用程序。我正在将来自服务器的评论加载到UITableViewCell 中的UITextView

这是用于将数据加载到 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 变成了一个垂直的单行列。

任何帮助将不胜感激!

【问题讨论】:

    标签: objective-c uitableview ios7 uitextview xcode5


    【解决方案1】:

    我认为这可能是原因:

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

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

    计算
    float widthOfTextView = textView.textContainer.size.width - horizontalPadding;
    

    在向下滚动和备份的情况下可能很小。

    【讨论】:

    • 嗨,我改变了heightForRowAtIndexPath 的高度,同样的问题发生了。我试过了,我发现这是[cell.commentTextView sizeToFit] 的问题..
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2014-11-05
    相关资源
    最近更新 更多