【问题标题】:heightForRowAtIndexPath: incorrect calcation when string contains newline \nheightForRowAtIndexPath:当字符串包含换行符时计算不正确\n
【发布时间】:2012-10-20 00:08:59
【问题描述】:

我在分组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?"

注意它包含\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);
}

其他不包含新行的字符串也可以正常工作。是否有理由在计算高度时不考虑换行并返回给我的高度太短?

谢谢

【问题讨论】:

  • 当你用'\n'计算高度时,它只考虑这两个字符。但是当它真正设置为标签的文本时,标签标识要放置为新行的字符。因此,当它呈现时,它会以不同的方式显示高度。
  • @R.A 这是不正确的。应该考虑换行符。
  • @runmad 你应该将你的标签的 numberOfLines 设置为零。(0) 尝试后告诉我它是否对你有帮助。 "label.numberOfLines = 0;"
  • 这可能对你有帮助.. stackoverflow.com/questions/2312899/…
  • @R.A 我已将 numberOfLines 设置为 0

标签: ios uitableview nsstring heightforrowatindexpath


【解决方案1】:

发布的代码可以正常工作并提供正确的结果。问题肯定出在其他地方。

也许self.view.frame 的宽度与UITableView 的边界不同?或者您忘记补偿分组单元格的边距或附件?我必须更多地了解您的UITableViewCell 和更具体的布局。

一个单元格,您的文本用于单元格的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);
}

【讨论】:

  • 是的,他没有使用标签宽度,它可能会有所不同,但是我使用的是标签/单元格宽度并且与 /n 有同样的问题。 @R.A 的评论是正确的。他还添加了边距。
  • 我使用的是UITableViewController,所以self.view.frame 是一样的。
  • 我不认为填充是这里的问题,因为它在非新行 NSStrings 上工作正常。
  • 我已经删除了我的反对票,因为这可能会导致他在路上出现问题,应该解决。
  • @runmad self.view.frame 将成为UITableView 的框架。您应该改用 tableView.bounds 并减去分组样式的填充 (2*10pt)。
猜你喜欢
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多