Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 表节标题:多行/换行_Swift_Uitableview_Header_Word Wrap - Fatal编程技术网

Swift 表节标题:多行/换行

Swift 表节标题:多行/换行,swift,uitableview,header,word-wrap,Swift,Uitableview,Header,Word Wrap,我正在尝试创建一个表,其中节标题可以是长字符串。我认为我的设置是正确的(动态行数、换行设置),但是字符串只是在末尾被截断。请注意,在其他地方,节标题的高度为80,这足以显示大约3行文本 // Format section header override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let header: UITa

我正在尝试创建一个表,其中节标题可以是长字符串。我认为我的设置是正确的(动态行数、换行设置),但是字符串只是在末尾被截断。请注意,在其他地方,节标题的高度为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

}

我做的正好相反,我将行数设置为一个巨大的数字,然后动态大小就可以工作了

  • 在IB中
  • 不要设置任何宽度/高度常量
  • 在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 {
另见

您必须创建一个自定义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
}    

指定自动高度标注-

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

你让它起作用了吗?如果知道您是如何解决的,我将不胜感激。我仍然有完全相同的问题。有人对此有解决方案吗?您如何不在故事板中设置任何宽度/高度常量?即使您尝试将标题的高度设置为0,它也会将您限制在1更正。我可以设置宽度,但我从未设置固定高度(例如高度=44),因此标签向下生长。如果我需要最小高度,我使用IB中的下拉菜单将其设置为大于或等于高度>=44。它可以工作,但IB可以发出警告。我认为内容拥抱需要调整,以获得IB 100%的批准
不要设置任何宽度/高度常量
这实际上对我有效。我删除了每个元素的所有高度常量,并为拥抱优先级设置了不同的值。它最终达到了预期的效果。