XIBファイルを作成します。
ファイル->新しいファイル-> ios-> cocoa touchクラス->次へ

「XIBファイルも作成する」にチェックマークを付けてください
と一緒に演奏したい tableview
ので、サブクラスを選択しましたUITableViewCell
あなたの必要条件として選択することができます

希望どおりのXIBファイル(RestaurantTableViewCell.xib)

各行の高さをテーブルに設定するには、行の高さをつかむ必要があります

さあ!それらを迅速にファイルをハックする必要があります。私はhuckedていますrestaurantPhoto
し、restaurantName
あなたはあなたのすべてをハックすることができます。

今UITableViewを追加しています

名前
nibファイルの名前。拡張子.nibを含める必要はありません。
オーナー
nibのファイルの所有者オブジェクトとして割り当てるオブジェクト。
オプション
nibファイルを開くときに使用するオプションを含む辞書。
最初に
定義していない場合は、最初にすべてのビューを取得するため、そのセット内の1つのビューを取得する必要があります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
}
}
あなたがやった:)
