如何在Swift中以编程方式将HeaderView从nib添加到UiTableView

Man*_*nta 4 uitableview uiscrollview ios swift

我是一个天真的IOS开发人员使用swift语言.

我有一个表格视图,显示酒店的功能列表.现在我想在tableView上添加一个标题信息与酒店图片,酒店名称在tableview上方,以便标题信息也与tableview内容一起滚动(产品功能列表) )

这是问题,TableView与功能列表工作正常.

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
    }


}
Run Code Online (Sandbox Code Playgroud)

对于像hotelImage,酒店名称和其他标签这样的标题信息,我创建了一个包含所有视图集的nib,以及一个映射到它的所有引用的相应类.

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

我搜索过但发现了如何设置字符串节标题的教程.

我尝试了几件事情:

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

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

但是如何在这里加载我的笔尖并设置为这个tableview,我也希望只有在从服务获取数据后才以编程方式触发

  1. 使用parent作为TableViewCell创建一个nib,使用dequeueReusableCellWithIdentifier()加载它,设置info并设置为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
    
    Run Code Online (Sandbox Code Playgroud)

但即使这是抛出一些异常,我也看不到异常的位置(如何在Xcode中查看异常日志?).

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

因为在Android中我曾经创建过一个带有线性布局的NestedScrollView,里面嵌入了任意数量的recyclerViews(去除了recyclelerviews的滚动)和Relativelayout

请帮我.

Ram*_*los 10

您可以使用此代码来初始化您的nib文件

let view = (NSBundle.mainBundle().loadNibNamed("MenuHeader", owner: self, options: nil)[0] as? UIView)
tableView.tableHeaderView = view
Run Code Online (Sandbox Code Playgroud)

那么你的MenuHeader将是一个普通的.xib文件,里面有你的单元格,只需记住适当调整约束以适应你想要的所有屏幕.