简体   繁体   English

SWIFT 3:如何在UITableViewController的列中添加标题?

[英]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 table​View(_:​view​For​Header​In​Section:​) 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. 换句话说,您的table​View(_:​view​For​Header​In​Section:​)实现需要返回一个视图 ,该视图包含三个并排的标签,分别是``日期'',``名称''和``金额'' ,就像您的单元格包含三个并排的标签一样。

在此处输入图片说明

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.

相关问题 如何在 Swift 中继承 UITableViewController - How to subclass UITableViewController in Swift 在 UITableViewController (Swift) 顶部添加按钮 - Add button on top of UITableViewController (Swift) 如何在 Swift 中添加滑动以从 UITableViewController 中删除表格单元格? - How to add swipe to remove table cells from UITableViewController in Swift? 尝试将带有标题的部分添加到UITableViewController(iOS) - Trying to add sections with headers to UITableViewController (iOS) 如何将uitoolbar添加到uitableviewcontroller? - How to add uitoolbar to uitableviewcontroller? 如何将ChildViewController添加到UITableViewController? - How to add ChildViewController to UITableViewController? 如何将选定的 UITableViewController 值添加到以前的 UITableViewController? - How to add a selected UITableViewController value to a previous UITableViewController? 在Swift中的UITableViewController顶部添加一个UIView - Add a UIView over the top of UITableViewController in Swift 我将UIScrollViewDelegate添加到UItableviewController swift时出错 - error when i add UIScrollViewDelegate to UItableviewController swift 如何将数据从UITableViewController传递到另一个UITableViewController? -iOS和Swift 2 - How to pass Data from a UITableViewController to another UITableViewController? -iOS & Swift 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM