我正在尝试创建一个表格,其中节标题可以是长字符串。我以为我有正确的设置(动态行数,自动换行集),但字符串在结尾处被截断。请注意,节标题的大小高度为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
}
答案 0 :(得分:0)
我相反,我将行数设置为一个巨大的数字,然后动态大小工作。
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?
答案 1 :(得分:0)
您必须创建自定义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 :(得分:0)
指定自动高度尺寸-
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}