2013-12-22 52 views
0

我有新的目标C编程,我目前正在尝试开发一个iOS应用程序。我正在将服务器上的注释加载到UITableViewCell中的UITextView中。调整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成为一个垂直的单行列。

任何帮助将不胜感激!

回答

0

我觉得这可能是原因:

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

您使用的是dequeued细胞的文本视图,以确定细胞的高度。然而dequeued小区可能不具有它的任何文本数据(或不同的文本),加上commentViewUIViewAutoresizingFlexibleWidth,宽度由

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

计算可以是向下滚动和备份的情景下非常小。

+0

嗨,我改变了'heightForRowAtIndexPath'的高度,同样的问题发生。我试过了,我发现它是'[cell.commentTextView sizeToFit]'是问题.. – user2909432