1

I have the following string that I insert in a grouped UITableViewCell:

NSString = @"In 1879, Steve Dancy sells his New York shop and ventures west to explore and write a journal about his adventures. Though he's not looking for trouble, Dancy's infatuation with another man's wife soon embroils him in a deadly feud with Sean Washburn, a Nevada silver baron.\n\nInfuriated by the outrages of two hired thugs, the shopkeeper kills both men in an impulsive street fight. Dancy believes this barbarian act has closed the episode. He is wrong. He has interfered with Washburn's ambitions, and this is something the mining tycoon will not allow.\n\nPinkertons, hired assassins, and aggrieved bystanders escalate the feud until it pulls in all the moneyed interests and power brokers in Nevada. Can the former city slicker settle accounts without losing his life in the process?"

Notice that it contains \n. When the string contains a new line I am returned the incorrect height in my heightForRowAtIndexPath: method:

#define FONT_SIZE 14.0f
#define CELL_CONTENT_MARGIN 14.0f

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; {
    CGSize constraint = CGSizeMake(CGRectGetWidth(self.view.frame) - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize size = [self.giftProduct.productDescription sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat height = MAX(size.height, 44.0f);
    return height + (CELL_CONTENT_MARGIN * 2);
}

Other strings that do not contain new lines work just fine. Is there are reason why it does not take the new line into consideration when calculating the height and returns too short of a height to me?

Thanks

Charles
  • 50,943
  • 13
  • 104
  • 142
runmad
  • 14,846
  • 9
  • 99
  • 140
  • when you calculate the height with '\n', it just considers that two characters. but when it really set as a text to the label, label identifies the characters to be placed as new line.so when it renders it shows the height differently. – Dinesh Raja Oct 30 '12 at 15:54
  • @R.A That's incorrect. The newline is taken into account, as it should. – Andreas Ley Oct 30 '12 at 16:12
  • @runmad you should set numberOfLines for your label to be set as zero.(0) Tell me if it helps you after tried it. "label.numberOfLines = 0;" – Dinesh Raja Oct 30 '12 at 16:24
  • this may help you.. http://stackoverflow.com/questions/2312899/how-to-add-line-break-for-uilabel – Dinesh Raja Oct 30 '12 at 16:27
  • @R.A I have set the `numberOfLines` to 0 – runmad Oct 30 '12 at 17:17

1 Answers1

0

The code posted works fine and provides the correct results. The problem has to be elsewhere.

Maybe self.view.frame has a different width than your UITableView's bounds? Or you forgot to compensate for the margin of grouped cells or for the accessory item? I'd have to know more about your UITableViewCell and layout to be more specific.

UITableViewCell grouped padding

A cell with your text used for the cell's textLabel.text and with its height calculated by this code:

#define FONT_SIZE 14.0f
#define CELL_CONTENT_MARGIN 10.0f  // label padding
#define CELL_MARGIN 10.0f  // cell margin for "grouped" style

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; {
    CGSize constraint = CGSizeMake(CGRectGetWidth(tableView.bounds) - (2 * CELL_MARGIN) - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize size = [self.testString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat height = MAX(size.height, 44.0f);
    return height + (CELL_CONTENT_MARGIN * 2);
}
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
  • Yes, he is not using the label width and it could be different, however I am using the label / cell width and am having the same issue with /n. @R.A is correct with his comment. Also he is adding margin padding. – Bot Oct 30 '12 at 16:10
  • I'm using a `UITableViewController` so `self.view.frame` would be the same. – runmad Oct 30 '12 at 16:19
  • I don't think the padding is the issue here, because it works fine on non-new line `NSStrings`. – runmad Oct 30 '12 at 16:20
  • I have removed my downvote since this could cause an issue down the road from him and should be fixed. – Bot Oct 30 '12 at 16:21
  • @runmad `self.view.frame` would then be the `UITableView`'s frame. You should use `tableView.bounds` instead and subtract the padding for the grouped style (2*10pt). – Andreas Ley Oct 30 '12 at 16:22
  • @runmad `self.view.frame` would not be the same if you have a `title` and `detailText` in your row. – Bot Oct 30 '12 at 16:22
  • @Bot you should set numberOfLines for your label to be set as zero.(0) Tell me if it helps you after tried it. "label.numberOfLines = 0;" – Dinesh Raja Oct 30 '12 at 16:24
  • @R.A yes it is number of lines = 0. I am waiting for my dev database to refresh from production so I can run a test. – Bot Oct 30 '12 at 16:25