如何在Swift中以编程方式将HeaderView从笔尖添加到UiTableView [英] How to Add HeaderView from nib to UiTableView programmatically in Swift

查看:153
本文介绍了如何在Swift中以编程方式将HeaderView从笔尖添加到UiTableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是一位使用敏捷语言的朴素的IOS开发人员.

Well i'm a naive IOS developer using swift language.

我有一个显示酒店特色列表的表视图.现在我想在表视图中添加带有酒店图像,表名上方的酒店名称的表头信息,以便表头信息也与表视图内容一起滚动(产品功能列表)

I have a table view which displays a list of features of a hotel.Now i want to add a header information to the tableView with hotel image, hotel name above the tableview so that the header information also scrolls along with tableview contents(product features list)

这是问题所在,带有功能列表的TableView正常工作.

Here is the problem, TableView with list of features is working fine.

class HotelDetailViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var categoriesTableView: UITableView!

    var items: [(String, String,String)] = [
        ("Service", "   Courteous service, Good staff , Good staff","   Bad Facility"),
        ("Vibe",  "   Courteous service, Good staff","   Bad Facility"),
        ("Hotel",  "   Courteous service, Good staff","   Bad Facility"),
        ("Room Facility",  "   Courteous service, Good staff","   Bad Facility"),
        ("Amenities",  "   Courteous service, Good staff","   Bad Facility"),
        ("Food", "   Courteous service, Good staff","   Bad Facility")
    ]


    override func viewDidLoad() {
        super.viewDidLoad()
        let nib = UINib(nibName: "HotelSnippetTableViewCell", bundle: nil)
        categoriesTableView.separatorStyle = UITableViewCellSeparatorStyle.None
        categoriesTableView.registerNib(nib, forCellReuseIdentifier: "CustomCell")
        categoriesTableView.rowHeight = UITableViewAutomaticDimension
        categoriesTableView.estimatedRowHeight = 100    
    }



    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let customTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! HotelSnippetTableViewCell

        let info  = items[indexPath.row]

        customTableViewCell.setCategory(info.0)

        customTableViewCell.setPositive(info.1)
        customTableViewCell.setNegative(info.2)


        return customTableViewCell

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        print("You selected cell #\(indexPath.row)!")

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }


}

对于诸如hotelImage,酒店名称和其他标签之类的标题信息,我创建了一个笔尖,其中包含所有视图集,并创建了一个对应类,其中所有引用都映射到了该类.

For header information like hotelImage , hotel name and other labels i have created a nib with all the views set and a corresponding class with all the references mapped to it.

现在如何以编程方式将此UiView作为标题添加到我的tableview中(因为我在rest调用之后获取数据).

Now how to add this UiView to my tableview as a header programatically (because i get data after rest call).

我已搜索但找到了有关如何设置字符串部分标题的教程.

I have searched but found tutorials on how to set string section headers.

我尝试过的事物:

  1. 我发现TableView有一个协议功能

  1. I found that there is a protocol function for TableView

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
       //Code}

但是如何在这里加载我的笔尖并设置为此tableview,我也希望ti仅在从服务中获取数据后以编程方式触发此操作

But how to load my nib here and set to this tableview and also i want ti trigger this programatically only after getting the data from service

  1. 创建一个笔尖,其父对象为TableViewCell,使用dequeueReusableCellWithIdentifier()加载它,设置信息并设置为tableview

  1. Create a nib with parent as TableViewCell, Load it using dequeueReusableCellWithIdentifier(), set info and set to tableview

    let headerCell =  categoriesTableView.dequeueReusableCellWithIdentifier("HotelDetailHeaderTableViewCell") as! HotelDetailHeaderTableViewCell  headerCell.setImageUrl("");

    headerCell.setHotelZmi(CGFloat(87))


    headerCell.setNameAndPersona("Fort Aguada Goa", persona: "OverAll", reviewsCount: "780")

    headerCell.frame = CGRectMake(0, 0, self.categoriesTableView.bounds.width, 600)

    categoriesTableView.tableHeaderView = headerCell

但是即使这样也会引发一些异常,我看不到异常在哪里(如何在Xcode中查看异常日志?).

But even this is throwing some exception and i can't see where the exception is(How to see exception log in Xcode?).

我正在执行的过程是否正确?如果没有人请给我建议有效的方法

Is the process i'm doing is correct? If not anyone please suggest me efficient approach

因为在Android中,我曾经使用嵌套的任意数量的recyclerViews(为recyclerviews移除滚动条)和Relativelayout来创建一个内嵌线性布局的NestedScrollView

Because in Android i used to create a NestedScrollView with linear layout inside it embedded with any number of recyclerViews(removing scroll for recyclerviews) and Relativelayout

请帮助我.

推荐答案

您可以使用此代码初始化您的笔尖文件

You can use this code to init your nib file

let view = (Bundle.main.loadNibNamed("MenuHeader", owner: self, options: nil)![0] as? UIView)

tableView.tableHeaderView = view

然后您的MenuHeader将是一个普通的.xib文件,其中包含您的单元格,只需记住适当调整约束以适合您想要的所有屏幕即可.

Then your MenuHeader will be a normal .xib file with your cell inside it, just remember to adjust the constraints appropriately to fit on all the screens that you want.

这篇关于如何在Swift中以编程方式将HeaderView从笔尖添加到UiTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆