2012-10-30 35 views
1

不正確calcation我有以下字符串,我在分組UITableViewCell插入:heightForRowAtIndexPath:當字符串包含換行符 n

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?" 

注意,它包含\ n。當字符串包含一個新行,我現在回到不正確的高度,我heightForRowAtIndexPath:方法:不包含新的生產線的工作只是罰款

#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); 
} 

其他字符串。在計算身高時,是否有理由不考慮新的線,並且返回的身高對我來說太短?

謝謝

+0

,當你計算與「\ n」的高度,它只是認爲兩個字符。但是當它真的被設置爲標籤的文本時,標籤會將要放置的字符標識爲新的線條。因此,當它呈現時,它會以不同的方式顯示高度。 –

+0

@ R.A這是不正確的。應該考慮換行符。 –

+0

@runmad你應該設置numberOfLines爲你的標籤設置爲零。(0)告訴我它是否可以幫你試過後。 「label.numberOfLines = 0;」 –

回答

0

代碼發佈工作正常,並提供正確的結果。問題必須在其他地方。

也許self.view.frame與您的UITableView的邊界有不同的寬度?或者你忘了補償分組電池的邊際或配件?我必須知道更多關於你的UITableViewCell和佈局更具體。

UITableViewCell grouped padding

與文本的單元格用於小區的textLabel.text和與此代碼來計算它的高度:

#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); 
} 
+0

是的,他不是使用標籤寬度,它可能是不同的,但我使用標籤/單元格寬度,並與/ n有相同的問題。 @ R.A對他的評論是正確的。他還補充了利潤率。 – Bot

+0

我正在使用'UITableViewController',所以'self.view.frame'會一樣。 – runmad

+0

我不認爲填充是這裏的問題,因爲它在非新行'NSStrings'上工作正常。 – runmad

相關問題