您应该有一个类来封装与单个表行/详细视图相关的所有信息。让我们称之为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]
}
}
这样,您只传递一个对象,而不必设置所有详细视图标签等。然后详细视图可以将同一对象传递到地图视图或其他视图。