[英]SWIFT 3: How to add headers to columns in UITableViewController?
I have a TableView in my app which contains 3 columns(Date, Name & Amount) whose values are fetched from Core data. 我的应用程序中有一个TableView,其中包含3列(日期,名称和金额),其值是从Core数据中获取的。 I am able to display these values in my App. 我可以在我的应用程序中显示这些值。
Now I want to provide fixed header names to every column which doesn't go on scrolling down the list. 现在,我想为每列提供固定的标题名称,而不会在向下滚动列表时使用。 But I am not getting any way of adding multiple headers to my table list. 但是我没有办法在表列表中添加多个标题。
Anyone has any idea about the same? 有人对此有任何想法吗?
Implemented method. 实现的方法。
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
let label = UILabel()
switch section {
case 0:
label.text="Date"
case 1:
label.text="Name"
case 2:
label.text="Amount"
default:
print()
}
view.addSubview(label)
return view
}
Adding screenshot of my Table View. 添加我的表格视图的屏幕截图。
为此,请使用tableview部分,使用以下方法传递您的自定义部分标题
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
You are doing correct just set the CGRect for label and view in viewForHeaderInSection and you can set the height of headerSection 您做得正确,只需在viewForHeaderInSection中设置标签和视图的CGRect即可设置headerSection的高度
public override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 61.0
}
But I am not getting any way of adding multiple headers to my table list 但是我没有任何办法将多个标头添加到我的表列表中
Correct. 正确。 You don't have multiple columns, really, either; 实际上,您也没有多个列; you have a single column with three things in each row. 您只有一列,每行三列。 Similarly, you need to make a section header which also has three things in a row. 同样,您需要制作一个节标题,该节标题还连续包含三项内容。 In other words, your tableView(_:viewForHeaderInSection:)
implementation needs to return a view containing three labels side by side, saying "Date", "Name", and "Amount", exactly as your cell contains three labels side by side. 换句话说,您的tableView(_:viewForHeaderInSection:)
实现需要返回一个视图 ,该视图包含三个并排的标签,分别是``日期'',``名称''和``金额'' ,就像您的单元格包含三个并排的标签一样。
Finally I managed to find the solution. 最后,我设法找到了解决方案。 It's about adding a View section just at the top of prototype. 这是关于在原型顶部添加一个View部分。 And then add labels/column name to that view. 然后将标签/列名称添加到该视图。 It will work. 它会工作。 No need to make any change in TableViewController class. 无需在TableViewController类中进行任何更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.