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

那么我是一个天真的iOS开发人员使用迅捷的语言。

我有一个表视图,它显示了一个酒店的function列表。现在我想添加一个标题信息到tableView与酒店图像,酒店名称上面的tableview,使标题信息也滚动tableview内容(产品function列表)

这是问题,TableView与function列表工作正常。

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 } } 

对于像酒店图像,酒店名称和其他标签的头信息,我已经创build了一个所有的视图设置一个笔尖和一个相应的类与所有参考映射到它的类。

现在如何将这个UiView以编程方式添加到我的tableview中(因为我在rest之后得到了数据)。

我已经search,但find如何设置string节标题教程。

我尝试了几件事情:

  1. 我发现TableView有一个协议函数

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

但是,如何加载我的笔尖在这里设置为这个tableview,我也想触发这个程序只有在从服务获取数据

  1. 创build一个父母作为TableViewCell,使用dequeueReusableCellWithIdentifier(),设置信息并设置为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 

但即使这是抛出一些exception,我不能看到exception是在哪里(如何看Xcodeexception日志?)。

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

因为在Android中,我用它创build了一个带有线性布局的NestedScrollView,里面embedded了任何数量的recyclerViews(移除滚动的recyclerviews)和Relativelayout

请帮帮我。

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

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

然后,你的MenuHeader将是一个普通的.xib文件,里面有你的单元格,只要记得适当地调整约束以适应所有你想要的屏幕。

迅速更新4

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

在你的viewForHeaderInSectionviewForHeaderInSection加载自定义视图并返回它

 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { // for example you have xib as 'CustomHeaderView.xib' let headerView = CustomView() headerView.myLabel.text = "My Title" return headerView } 

另一种方法是指定标题高度

 func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 60.0 // put your height here }