【问题标题】:Align Custom UITableViewCell content with Header将自定义 UITableViewCell 内容与 Header 对齐
【发布时间】:2015-06-07 04:04:45
【问题描述】:

我正在创建一个带有静态单元格的表格视图。有些单元格需要自定义样式,因为左侧包含一个标签,右侧包含一个文本字段。

我想像基本单元格样式一样将左侧的标签与部分的标题对齐,但我找不到这样做的方法。

这是我的应用目前的样子:

这是基本样式中对齐标签的样子:

我正在使用带有大小类和约束的自动布局。

【问题讨论】:

  • 你在 cellForRowAtIndexPath 中设置框架吗?
  • 不,我应该这样做吗?
  • 是的,我猜。它不是解决它的最佳方法。但是一旦我做了这种事情,因为我没有找到任何解决方案。也只有设置框架在自动布局模式下不起作用,有时它可能不会改变视图框架。尝试为要更改位置的视图设置cell.titleLabel.translatesAutoresizingMaskIntoConstraints = YES;。或者也尝试使用自动布局找到它,如果有什么办法?如果你找到了,告诉我。:)

标签: ios uitableview


【解决方案1】:

我也遇到过类似的问题。

我的要求是,

1) 创建一个包含标题和单元格的表格视图。

2) 标题将有 3 个标签,在这些标签下,单元格将保存标题中各个标签的相应数据。

为了实现上述要求,我在页眉中设置了标签,并根据页眉中的标签位置和大小,根据页眉标签重新设置了单元格的标签。

我在TableView的委托方法- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath中实现了上面的东西

并根据标题在其中的位置和大小重新构建每个单元格的标签。

例如-

-(void) tableView:(UITableView *) tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
         // customize the cell just before it will be displayed

         // access section header
         UIView *sectionHeader = [tableView headerViewForSection:indexPath.section];

         // access section header label
         UILabel *headerLeftLabel = (UILabel *)[sectionHeader viewWithTag:HEADER_LABEL_TAG];

         // access cell's corresponding label which is to be aligned as the header's above label 
         UILabel *leftCellLabel = (UILabel *)[cell.contentView viewWithTag:CELL_LABEL_TAG];

         // align/ reframe the cells label with the corresponding label in the header
         [leftCellLabel setFrame: headerLeftLabel.frame];

         // similar for other labels

}

【讨论】:

    【解决方案2】:

    感谢您的帮助,Xcode 6.3 似乎已自动解决此问题

    【讨论】:

    【解决方案3】:

    您可能可以使用basic / default UITableViewCell 样式,然后将文本字段设置为单元格的accessoryView

    使用basic 样式将自动排列节标题和每个单元格中的文本。

    Put a UITextField inside a UITableViewCell's AccessoryView

    【讨论】:

      【解决方案4】:

      节标题或系统定义Right Detail样式单元格的前导空间,在不同的屏幕方向下是不同的。纵向模式下为 16 点,横向模式下为 20 点。而Detail 标签的尾随空格始终为 15 个点。

      目前我所做的是为自定义单元格中每个视图的主要锚约束创建出口,并通过以下方法更改它们的常量:

      override func viewWillLayoutSubviews() {
          super.viewWillLayoutSubviews();
          // 16 in portrait mode, and 20 in landscape mode, act as the baseline
          let spacing = titleLabelOfRightDetailCell.frame.origin.x; 
          // leadingAnchorConstraints is an array of views within custom table view cells that need to be left aligned
          for constraint in leadingAnchorConstraints {
              constraint.constant = spacing;
          }
      }
      

      这样我可以确保所有表格视图单元格左对齐,无论是纵向模式还是横向模式。

      编辑:

      找到了更优雅的解决方案:Matching left alignment of custom & standard UITableViewCell types

      【讨论】:

        【解决方案5】:

        请看下面的帖子。 Matching left alignment of custom & standard UITableViewCell types问题及解决方案概述

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-11-26
          • 2012-01-20
          • 1970-01-01
          • 1970-01-01
          • 2020-01-24
          • 1970-01-01
          • 2015-01-30
          相关资源
          最近更新 更多