単一のテーブル行/詳細ビューに関連するすべての情報をカプセル化するクラスが必要です。と呼びましょうmodel
。
テーブル ビュー コントローラーでは、model
s の配列があります。
var models = [model]()
cellForRowAtIndexPath
特定のモデルに基づいてセルを返すようにオーバーライドします。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("someCellId", forIndexPath: indexPath) as! UITableViewCell
let model = models[indexPath.row]
// Set the title of the cell to be the title of the logItem
cell.textLabel?.text = model.name
cell.detailTextLabel?.text = model.details
return cell
}
ストーリーボードで詳細ビューへのセグエを作成し、全体model
を詳細ビューに渡します
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if (segue.identifier == "segueToDetail") {
var svc = segue.destinationViewController as! DetailViewController
svc.model = models[tableView.indexPathForSelectedRow()!.row]
}
}
この方法では、1 つのオブジェクトのみを渡し、すべての詳細ビュー ラベルなどを設定する必要はありません。また、詳細ビューは同じオブジェクトをマップ ビューまたは他のビューに渡すことができます。