Background:
I have a Master-Detail view split view controller. In detail table view, I have a static table view with a cell containing only one UITextView to show the comment of a customer.
When I tap customer name in the Master view, it reads the comment information and set it in the text in UITextView in detail view's viewWillAppear. At the same time, the UITextView resize with following code:
CGRect frame;
frame = self.detailCustomerCommentUITextView.frame;
frame.size.height = [self.detailCustomerCommentUITextView contentSize].height;
self.detailCustomerCommentUITextView.frame = frame;
[self.tableView reloadData];
The height of this cell is changed as well:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height = 30.0;
if (indexPath.section==3&&indexPath.row==1)
{
height = MAX(self.detailCustomerCommentUITextView.frame.size.height, 30.0);
}else if (indexPath.section == 0 && indexPath.row == 0)
{
height = 55.0;
}else if (indexPath.section == 1 && indexPath.row == 0)
{
height = 47.0;
}
return height;
}
Steps to reproduce the issue:
The first time I tap a customer (Customer Short), which has a short comment text. The resized frame is: (CGRect) frame = origin=(x=67, y=-6) size=(width=568, height=32)
I got this value in breakpoint after: self.detailCustomerCommentUITextView.frame = frame;
Then in heightForRowAtIndexPath, I got the same height:
(CGFloat) height = 32
This looks good, but in the app I saw no comment in UITextView!
I am pretty sure this text view's text is not empty as I checked this in debug mode already.
Then I tap the same customer again, in Break point after:
frame = self.detailCustomerCommentUITextView.frame;
I saw (CGRect) frame = origin=(x=67, y=-6) size=(width=568, height=10)
This means after the last frame resizing to height "32", the UITextView is resized to height "10".
Then the code runs through
frame.size.height = [self.detailCustomerCommentUITextView contentSize].height;
self.detailCustomerCommentUITextView.frame = frame;
I saw height is set to 32 again. But this time everything is perfect. I saw the comment text and the UITextView is resized:
Then I repeated the above steps, and see everything is fine and the UITextView is not resized any more.
Question Summary:
In my first tapping a customer Short, why the UITextView is resized to height 10 after it is successfully resized to height 32?
I used single step Debug but cannot find any other code that changes the frame.
Highly appreciate your kind help.
Issue resolved. I found although the UITextView is resized, when it calls heightForRowAtIndexPath, the UITextView's frame is overlapped by the new height of cell.
In storyboard, UITextView -> Size Inspector -> View -> Autosizing, I remove all inside arrow, leaving only outside I(Sorry I cannot tell which is Struts and which is Springs). By this way, the UITextView will be auto resize to fit the new height of cell.
Hope this helps to anyone needs.