创建一个XIB文件:
文件->新文件-> iOS->可可触摸类->下一步

确保选中标记“还创建XIB文件”
我想表演,tableview
所以我选择了子类UITableViewCell
您可以选择作为您的需求

如您所愿的XIB文件(RestaurantTableViewCell.xib)

我们需要抓住行高来设置表格每一行的高度

现在!需要把他们快速地藏起来。我被欺骗了restaurantPhoto
,restaurantName
你可以欺骗所有人。

现在添加一个UITableView

name
nib文件的名称,不需要包含.nib扩展名。
owner
分配为笔尖文件的Owner对象的对象。
options
字典,其中包含打开nib文件时要使用的选项。
第一
,如果你不首先定义掠然后所有视图..所以,你需要抓住这组内的一个观点frist
。
Bundle.main.loadNibNamed("yourUIView", owner: self, options: nil)?.first as! yourUIView
这是表视图控制器的完整代码
import UIKit
class RestaurantTableViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let restaurantTableviewCell = Bundle.main.loadNibNamed("RestaurantTableViewCell", owner: self, options: nil)?.first as! RestaurantTableViewCell
restaurantTableviewCell.restaurantPhoto.image = UIImage(named: "image1")
restaurantTableviewCell.restaurantName.text = "KFC Chicken"
return restaurantTableviewCell
}
// set row height
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
}
你完成了:)
