2015-09-30 48 views
0

UICollectionViewCell高度增加正確的iPhone 6

我使用UICollectionView和使用自定義UICollectionViewCell。用戶創建的帖子顯示在新聞源屏幕上。基於文本的長度,我正在增加單元格的高度。但在「說些什麼」文本字段下會出現奇怪的間距。

當用戶通過按ENTER鍵輸入文本時,它工作得很好。如果用戶輸入多行而沒有按ENTER鍵,則會發生問題。

此代碼完美適用於iPhone 5,但不適用於iPhone 6和6 plus。自上一週以來,我一直在困擾這個問題。這個問題僅與iPhone 6和6 Plus有關嗎?

我沒有找到有關我的問題的相關問題,這就是爲什麼問一個新的問題。

- (void)configureCell:(NewsFeedObj *)newsFeed{ 
self.layer.borderWidth = 1; 
self.layer.borderColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1].CGColor; 
self.btnPostComment.enabled   = NO; 
self.btnPostComment.titleLabel.textColor = [UIColor colorWithRed:191.0/255.0 green:191.0/255.0 blue:191.0/255.0 alpha:1.0]; 
self.postid      = newsFeed.postId; 
float yPosition = 0; 
//*********************Post Username and tagged users setting********* 

self.userName.enabledTextCheckingTypes = NSTextCheckingTypeLink; // Automatically detect links when the label text is subsequently changed 
self.userName.shadowColor = [UIColor whiteColor]; 
self.userName.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol) 
self.userName.numberOfLines = 4; 
self.userName.lineSpacing = 5; 
[self settingUserSectionDetailWithNewsFeed:newsFeed]; 

CGRect frame = self.userName.frame; 

frame.size.height = [self.userName sizeThatFits:self.userName.frame.size].height+3; 

self.userName.frame = frame; 

//set Time and locatin postion accordint to the name lable. 
[self.timeLocationView setFrame:CGRectMake(self.timeLocationView.frame.origin.x, (self.userName.frame.origin.y+self.userName.frame.size.height)+3, self.timeLocationView.frame.size.width, self.timeLocationView.frame.size.height)]; 
[self.userSection setFrame:CGRectMake(self.userSection.frame.origin.x, self.userSection.frame.origin.y, self.userSection.frame.size.width, self.timeLocationView.frame.origin.y+self.timeLocationView.frame.size.height+5)]; 
//****************************************user Location setting 
if (newsFeed.postLocation.length>0) { 
    self.location.hidden = NO; 
    self.locationIconImg.hidden = NO; 
    self.location.text = newsFeed.postLocation; 
}else{ 
    self.location.hidden = YES; 
    self.locationIconImg.hidden = YES; 
} 

//**********************Check Post Spam/Offensive/Or Post is yourself******************** 
if (newsFeed.postUser.user_id == [ConnectionManager getInstance].userProfile.user_id) { 
    [self.btnFlag setImage:[UIImage imageNamed:@"edit-report"] forState:UIControlStateNormal]; 
}else{ 
    if (newsFeed.postReportFromCurrentUser) { 
     if ([newsFeed.postReportFromCurrentUser[@"kind"] isEqualToString:@"offensive"] || [newsFeed.postReportFromCurrentUser[@"kind"] isEqualToString:@"spam"]) { 
      [self.btnFlag setImage:[UIImage imageNamed:@"report_active"] forState:UIControlStateNormal]; 
     }else{ 
      [self.btnFlag setImage:[UIImage imageNamed:@"report_inactive"] forState:UIControlStateNormal]; 
     } 
    } 
} 
//post Time setting 
self.dateAdded.text    = [self timeLeftSinceDate:[self getDateFromString: newsFeed.postCreatedAt]]; 
//Post Edit long gesture 
UILongPressGestureRecognizer *postTextEdit = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(textEditPost:)]; 
[self.description addGestureRecognizer:postTextEdit]; 
//***********************************post User profile image setting 
NSDictionary *postUserImgDict = newsFeed.postUser.avatar[@"medium"]; 
NSString *userImgUrl   = @""; 
if (![postUserImgDict[@"url"] isMemberOfClass:[NSNull class]]) { 
    userImgUrl = postUserImgDict[@"url"]; 
} 
[self.profileImage sd_setImageWithURL:[NSURL URLWithString:userImgUrl] placeholderImage:[UIImage imageNamed:@"avatar"]]; 
[self.profileImage setupImageViewer]; 
//*************************************Post text setting here 
self.description.text   = newsFeed.postBody; 
CGSize rect; 
if (![newsFeed.postBody isEqualToString:@""]) { 
    /*NSDictionary *attributes = @{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:14.0f]}; 
    NSAttributedString *descString = [[NSAttributedString alloc] initWithString:self.description.text attributes:attributes]; 
    self.description.attributedText = descString;*/ 

    [self.description setAttributes: [NSDictionary dictionaryWithObject:[UIFont fontWithName:@"Arial" size:14.0f] forKey:NSFontAttributeName]]; 
    rect      =[self.description suggestedFrameSizeToFitEntireStringConstrainedToWidth:self.userSection.frame.size.width-20]; 
    //rect      = [self.description sizeThatFits:CGSizeMake(self.userSection.frame.size.width-20, CGFLOAT_MAX)]; 

    /*if (self.userSection.height>60) { 
    [description setFrame:CGRectMake(self.userSection.frame.origin.x+10, self.userSection.frame.origin.y+self.userSection.frame.size.height+10, self.userSection.frame.size.width-20, rect.height+10)]; 
    }else{ 
    [description setFrame:CGRectMake(self.userSection.frame.origin.x+10, self.userSection.frame.origin.y+self.userSection.frame.size.height+10, self.userSection.frame.size.width-20, rect.height+10)]; 
    }*/ 

    [description setFrame:CGRectMake(self.userSection.frame.origin.x+10, self.userSection.frame.origin.y+self.userSection.frame.size.height+10, self.userSection.frame.size.width-20, rect.height+10)]; 

    if(IS_IPHONE_5) { 
     yPosition = self.description.frame.origin.y+self.description.frame.size.height; 
    } 
    else { 
     yPosition = self.description.frame.origin.y + (rect.height + 10); 
    } 

}else{ 
    yPosition = self.userSection.frame.origin.y+self.userSection.frame.size.height+10; 
} 
//************************************************************************* 
//post Image setting here 

if(![newsFeed.postImageUrl isMemberOfClass:[NSNull class]] && ![newsFeed.postImageUrl isEqualToString:@""]){ 

    [self.postedImage sd_setImageWithURL:[NSURL URLWithString:newsFeed.postImageUrl] placeholderImage:nil]; 
    self.postedImage.hidden = NO; 
    self.postedImage.frame  = CGRectMake(0, yPosition, self.postedImage.frame.size.width, self.postedImage.frame.size.height); 
    yPosition = self.postedImage.frame.origin.y+self.postedImage.frame.size.height; 

}else{ 
    self.postedImage.hidden = YES; 
} 
//***************************Rating Section ************************************ 
self.ratingSection.frame   = CGRectMake(self.ratingSection.frame.origin.x, yPosition, self.ratingSection.frame.size.width, self.ratingSection.frame.size.height); 

yPosition = self.ratingSection.frame.origin.y+self.ratingSection.frame.size.height; 
//***************************RatingCOunt Section ************************************ 
self.countSection.frame   = CGRectMake(self.countSection.frame.origin.x, yPosition, self.countSection.frame.size.width, self.countSection.frame.size.height); 
if (newsFeed.postRatingAction.count>0) { 

    self.lblAwesome.text = [NSString stringWithFormat:@"%ld .",(long)[newsFeed.postRatingAction[0] integerValue]]; 
    self.lblWhatevs.text = [NSString stringWithFormat:@"%ld .",(long)[newsFeed.postRatingAction[1] integerValue]]; 
    self.lblNoBueno.text = [NSString stringWithFormat:@"%ld .",(long)[newsFeed.postRatingAction[2] integerValue]]; 
    if (newsFeed.postReactionFromCurrentUser.count>0) { 
     if ([newsFeed.postReactionFromCurrentUser[@"kind"] isEqualToString:@"awesome"]) { 
      self.btnAwesome.selected = YES; 
      self.btnWhatEvs.selected = NO; 
      self.btnNoBueno.selected = NO; 
     }else if ([newsFeed.postReactionFromCurrentUser[@"kind"] isEqualToString:@"whatevs"]) { 
      self.btnAwesome.selected = NO; 
      self.btnWhatEvs.selected = YES; 
      self.btnNoBueno.selected = NO; 
     }else if ([newsFeed.postReactionFromCurrentUser[@"kind"] isEqualToString:@"nobueno"]){ 
      self.btnAwesome.selected = NO; 
      self.btnWhatEvs.selected = NO; 
      self.btnNoBueno.selected = YES; 
     } 
    }else{ 
     self.btnAwesome.selected = NO; 
     self.btnWhatEvs.selected = NO; 
     self.btnNoBueno.selected = NO; 
    } 
} 
yPosition = self.countSection.frame.origin.y+self.countSection.frame.size.height; 

//***************************Comment Section ************************************ 
//if comment against post not empty 
if (newsFeed.commentsArray.count>0) { 
    self.commentSectionContainer.hidden = NO; 
    float commenty=0; 
    self.lblComments.text = [NSString stringWithFormat:@"%lu",(unsigned long)newsFeed.commentsArray.count]; 
    for (postComment *comment in newsFeed.commentsArray) { 
     commentSection *commentView = [[[NSBundle mainBundle] loadNibNamed:@"TextComment" 
                    owner:nil options:nil] objectAtIndex:0]; 
     commentView.editCommentView.txtComment.delegate = self; 
     commentView.tag    = 1000+[newsFeed.commentsArray indexOfObject:comment]; 
     commentView.userInteractionEnabled = YES; 
     [commentView.profileImg setupImageViewer]; 
     commentView.profileName.text = comment.UserName; 
     [self commentNameSettingWithLabel:(TTTAttributedLabel *)commentView.profileName]; 
     CGSize stringsize = [comment.UserName sizeWithFont:[UIFont boldSystemFontOfSize:14]]; 
     [commentView.profileName setFrame:CGRectMake(commentView.profileName.frame.origin.x, commentView.profileName.frame.origin.y, stringsize.width+5, commentView.profileName.frame.size.height)]; 
     commentView.profileName.shadowColor = [UIColor clearColor]; 
     commentView.lblComentTime.frame = CGRectMake(commentView.profileName.frame.origin.x+commentView.profileName.frame.size.width, commentView.lblComentTime.frame.origin.y, commentView.lblComentTime.frame.size.width, commentView.lblComentTime.frame.size.height); 
     //Set comment time here 
     commentView.lblComentTime.text = [self timeLeftSinceDate:[self getDateFromString:comment.created_at]]; 

     [commentView.profileImg sd_setImageWithURL:[NSURL URLWithString:comment.UserProfileImg] placeholderImage:[UIImage imageNamed:@"avatar"]]; 

     if (comment.isEditComment) { 
      //Add edit view for edit comment 

      commentView.editCommentView.txtComment.layer.borderColor = [UIColor lightGrayColor].CGColor; 
      commentView.editCommentView.txtComment.layer.borderWidth = 0.5; 
      commentView.lblCommentText.hidden = YES; 
      commentView.editCommentView.hidden = NO; 
      commentView.editCommentView.txtComment.editable = YES; 
      commentView.lblCommentText.text  = comment.text; 
      commentView.editCommentView.txtComment.text = comment.text; 
      CGSize comentRect      =[commentView.lblCommentText suggestedFrameSizeToFitEntireStringConstrainedToWidth:commentView.frame.size.width]; 

      [commentView.editCommentView.txtComment setFrame:CGRectMake(commentView.editCommentView.txtComment.frame.origin.x, commentView.editCommentView.txtComment.frame.origin.y, commentView.editCommentView.txtComment.frame.size.width, comentRect.height+15)]; 
      [commentView.editCommentView setFrame:CGRectMake(commentView.editCommentView.frame.origin.x, commentView.editCommentView.frame.origin.y, commentView.editCommentView.frame.size.width, commentView.editCommentView.txtComment.frame.origin.y+commentView.editCommentView.txtComment.frame.size.height)]; 
      [commentView setFrame:CGRectMake(self.sharingSection.frame.origin.x, commenty, self.frame.size.width, commentView.editCommentView.frame.origin.y+commentView.editCommentView.frame.size.height+10)]; 
      commentView.editCommentView.userInteractionEnabled = YES; 
      if(commentView.editCommentView.txtComment.isEditable){ 
       [commentView.editCommentView.txtComment becomeFirstResponder]; 
      } 

     }else{ 
      commentView.lblCommentText.hidden = NO; 
      commentView.editCommentView.hidden = YES; 
      commentView.lblCommentText.text = comment.text; 
      CGSize comentRect      =[commentView.lblCommentText suggestedFrameSizeToFitEntireStringConstrainedToWidth:commentView.frame.size.width-20]; 
      //coment Frame setting 
      [commentView.lblCommentText setFrame:CGRectMake(commentView.lblCommentText.frame.origin.x, commentView.lblCommentText.frame.origin.y, commentView.lblCommentText.frame.size.width, comentRect.height)]; 
      [commentView setFrame:CGRectMake(self.sharingSection.frame.origin.x, commenty, self.frame.size.width, commentView.lblCommentText.frame.origin.y+commentView.lblCommentText.frame.size.height+10)]; 
     } 
     commentView.userInteractionEnabled = YES; 
     if (comment.commentUserid == [ConnectionManager getInstance].userProfile.user_id) { 
      //long Press for edit or deleted comment 
      UILongPressGestureRecognizer *cmttap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(commentEditOrDelete:)]; 
      [commentView addGestureRecognizer:cmttap]; 
     } 

     commenty = commentView.frame.origin.y+commentView.frame.size.height; 
     commentView.sepratorView.frame = CGRectMake(0, commentView.frame.size.height-1, commentView.frame.size.width, 1); 
     [self.commentSectionContainer addSubview:commentView]; 
     self.commentSectionContainer.frame = CGRectMake(self.commentSectionContainer.frame.origin.x, yPosition, self.frame.size.width, commentView.frame.origin.y+commentView.frame.size.height); 
     [self setCommentLabelFontwitLabel:commentView.lblCommentText]; 
    } 

    yPosition = self.commentSectionContainer.frame.origin.y+self.commentSectionContainer.frame.size.height; 
}else{ 
    self.commentSectionContainer.hidden = YES; 
    self.lblComments.text = @"0"; 
} 
//***************************Post Comment Section ************************************ 
self.txtComentView.text    = @""; 
//self.postCommentSection.layer.borderWidth = 1.0; 
//self.postCommentSection.layer.borderColor = [UIColor redColor].CGColor; 

self.postCommentSection.frame  = CGRectMake(self.postCommentSection.frame.origin.x, yPosition, self.postCommentSection.frame.size.width, self.postCommentSection.frame.size.height); 
yPosition      = self.postCommentSection.frame.origin.y+self.postCommentSection.frame.size.height; 


self.cellTotalHeight = yPosition; 
[self setTweetLabelFont]; 

}

+0

可能這與您的自動佈局有關。你在使用大小班嗎? – Rhenz

+0

我沒有使用自動佈局。 –

回答

0

通過創建3個不同的廈門國際銀行文件的iPhone 5,6和6個加終於解決了問題。