2013-12-22 81 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