【问题标题】:Table section header: multi-line/word wrapping表格部分标题:多行/自动换行
【发布时间】:2015-07-01 20:49:19
【问题描述】:

我正在尝试制作一个表,其中部分标题可以是长字符串。我以为我的设置是正确的(动态行数,自动换行设置),但字符串最后被截断了。请注意,节标题的大小在其他地方设置为 80 的高度,这足以显示大约 3 行文本。

// Format section header
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = mainColorBlue
    header.textLabel.textColor = UIColor.whiteColor()

    header.textLabel.textAlignment = NSTextAlignment.Left
    header.textLabel.numberOfLines = 0 // Dynamic number of lines
    header.textLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
    header.textLabel.font = UIFont(name: "HelveticaNeue-Thin", size: 16)!
    header.textLabel.text = objectsArray[section].sectionName

}

【问题讨论】:

  • 你让它工作了吗?希望知道您是如何解决的。
  • 我仍然遇到同样的问题
  • 有没有人可以解决这个问题?

标签: swift uitableview header word-wrap


【解决方案1】:

你必须创建一个自定义的 headerView。这就是我所做的 - Swift 3,您必须对其进行自定义以供使用:

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//Need to create a label with the text we want in order to figure out height
    let label: UILabel = createHeaderLabel(section)
    let size = label.sizeThatFits(CGSize(width: view.width, height: CGFloat.greatestFiniteMagnitude))
    let padding: CGFloat = 20.0
    return size.height + padding
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UITableViewHeaderFooterView()
    let label = createHeaderLabel(section)
    label.autoresizingMask = [.flexibleHeight]
    headerView.addSubview(label)
    return headerView
}

func createHeaderLabel(_ section: Int)->UILabel {
    let widthPadding: CGFloat = 20.0
    let label: UILabel = UILabel(frame: CGRect(x: widthPadding, y: 0, width: self.view.width - widthPadding, height: 0))
    label.text = sectionTextArray[section]// Your text here
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignment.left
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.subheadline) //use your own font here - this font is for accessibility 
    return label
}    

【讨论】:

    【解决方案2】:

    指定自动高度尺寸 -

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    

    【讨论】:

      【解决方案3】:

      我做相反的事情,我将行数设置为一个巨大的数字,然后动态大小起作用。

      • 在 IB 中
      • 不要设置任何 WIDTH/HEIGHT 常量
      • TableViewCells 中的控件周围只有前导尾随/顶部/底部

      self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension; self.tableView.estimatedSectionHeaderHeight = 25;

      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          //-----------------------------------------------------------------------------------
          //DYNAMIC TEXT and TABLE ROW HEIGHT
          //-----------------------------------------------------------------------------------
          self.tableView.estimatedRowHeight = 80.0f;
          //also done in heightForRowAtIndexPath
          self.tableView.rowHeight = UITableViewAutomaticDimension;
      
          self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
          self.tableView.estimatedSectionHeaderHeight = 80.0f;
      
      }
      //comment out
      //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
      

      另请参阅 Is it possible to obtain a dynamic table view section header height using Auto Layout?

      【讨论】:

      • 如何不在情节提要中设置任何宽度/高度常量?即使您尝试将标题的高度设置为 0,它也会将您限制在 1
      • 更正我可以设置宽度,但我从不设置固定高度(例如高度 = 44),因此标签向下增长。如果我需要一个最小高度,我使用 IB 中的下拉菜单将其设置为大于或等于高度 >= 44。它可以工作,但 IB 可以用警告来抱怨。我认为拥抱内容需要调整才能获得 IB 100% 的批准
      • Dont set any WIDTH/HEIGHT constants 这实际上对我有用。我删除了每个元素的所有高度常量,并为拥抱优先级设置了不同的值。终于如期而至。
      猜你喜欢
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多