iphone - 在-tableView :heightForRowAtIndexPath: when there is a section index present期间获取tableview单元格的真实宽度

标签 iphone uitableview

我的自定义 UITableViewCells 的正确高度取决于它们的宽度。不幸的是,很难知道 -tableView:heightForRowAtIndexPath: 中单元格的实际内容区域宽度,因为我必须使用的唯一信息是表格 View 。有几个因素可以改变内容宽度,因此它不等于tableView.bounds.size.width;我现在正在努力解决的是部分索引。 (另一个是表格 View 是否分组。)

一般有没有一种好的方法可以从 tableView 获取单元格内容宽度?

如果做不到这一点,有没有办法获得部分索引的宽度,以便我可以从边界宽度中减去它? (我不想对其进行硬编码,因为这无法跨设备移植。)

注意:这不是 Need access to the cell in the tableView:heightForRowAtIndexPath: 的重复因为解决方案就是使用边界。它也不是 Get tableView:heightForRowAtIndexPath: to happen after tableView:cellForRowAtIndexPath:? 的重复。因为解决方案是硬编码一个常量 (265.0),该常量不可跨设备移植。

最佳答案

这就是我的工作内容。它很丑陋,但它有效,至少在我能找到的所有情况下都是如此。

- (CGFloat)sectionIndexWidthForSectionIndexTitles:(NSArray *)titles {
  UIFont *sectionIndexFont = [UIFont fontWithName:@"Helvetica-Bold" size:14.0f];
  CGFloat maxWidth = CGFLOAT_MIN;
  for(NSString *title in titles) {
    CGFloat titleWidth = [title sizeWithFont:sectionIndexFont].width;
    maxWidth = MAX(maxWidth, titleWidth);
  }

  CGFloat sectionIndexWidth = 0.0f;
  NSUInteger maxWidthInt = (int)maxWidth;
  switch(maxWidthInt) {
    case 0:
      sectionIndexWidth = 0.0f;
      break;
    case 11:
      sectionIndexWidth = 30.0f;
      break;
    case 12:
      sectionIndexWidth = 31.0f;
      break;
    case 14:
      sectionIndexWidth = 32.0f;
      break;
    default:
      sectionIndexWidth = 0.0f;
      break;
  }

  return sectionIndexWidth;  
}

关于iphone - 在-tableView :heightForRowAtIndexPath: when there is a section index present期间获取tableview单元格的真实宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3647242/

相关文章:

swift - 当表不可见时防止/推迟从 NSFetchedResultsController 更新 TableView

ios - UITableViewCell 和标签不显示文本

objective-c - UITableView 不重新加载内容

iphone - 取消隐藏 View 时,becomeFirstResponder 会减慢应用程序的速度

iphone - 用于我的 iPhone 应用程序的 Google Places API

iPhone AES Rijndael 128 解密

swift - 在 Swift 中创建 CSV 文件并写入文件

ios - UISearchBar 未设置为 UITableView.tableHeaderView

iphone - 当 coreplot 中触及某个点时,如何获取该点的信息?

iphone - ScrollView 在单击按钮时上下滚动