创建 XIB 文件:
文件->新建文件->ios->可可触摸类->下一个
确保勾选“同时创建 XIB 文件”
我想和tableview一起表演所以我选择了子类UITableViewCell
您可以选择作为您的要求
按您的意愿设计 XIB 文件 (RestaurantTableViewCell.xib)
我们需要抓取行高来设置表格每一行的高度
现在!需要把他们快速归档。我被restaurantPhoto 和restaurantName 搞砸了,你们可以搞砸你们所有人。
现在添加一个 UITableView
姓名
nib 文件的名称,不需要包含 .nib 扩展名。
所有者
分配为 nib 的 File's Owner 对象的对象。
选项
包含打开 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
}
}
你完成了:)