无法在容器视图中填充 table 视图
Can't populate a table view in a Container view
我有一个带有分段控制元素的 UIViewController。在此之下,我有两个重叠的容器视图,它们会自动在情节提要中创建各自的视图。我可以毫无问题地将这些视图链接起来并呈现所选分段控件的每个视图。
我的问题出在我尝试在其中一个容器视图中实现 table 视图并传递数据以填充视图时。为了分解这个,假设我有 3 个视图控制器。
vc1 是 UITableViewController 类型:包含 table 个类别。
vc2 是 UI 类型ViewController:包含分段控件和两个重叠的容器视图。
vc3 的类型为 UIViewController:是具有 UITableView 的容器视图之一(我已从故事板中拖入)。此 table 显示通过 vc1 中的单元格选择的类别中的项目列表。
通常情况下,我可以使用适当的数据填充 vc3 以显示 table 的项目列表,只需使用 vc1 中的 prepareForSegue 方法即可,但由于这些之间有一个额外的 ViewController两个 ViewControllers 我没能完成相同的结果。
整体 objective 是用 vc1 中所选单元格的适当数据填充 tablevc3 中的视图。
分段视图控制器是 VC2 - 在示例中我提到了一个类别。在代码中实际上是一个包含目的地的 Trip。因此,该类别是一次旅行。类别中的项目作为旅行中的目的地(即目的地是填充 vc3 中的 table)
import UIKit
class SegmentedViewController: UIViewController {
var trip: Trip!
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var itineraryContainer: UIView!
@IBOutlet weak var plannerContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = trip.tripName
itineraryContainer.hidden = true
//Setting the initial container to be viewed to match the segmentedcontrol
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func segmentedControlAction(sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
plannerContainer.hidden = false
itineraryContainer.hidden = true
break
case 1:
plannerContainer.hidden = true
itineraryContainer.hidden = false
break
default:
break
}
}
}
Planner 视图控制器(容器
import UIKit
class PlannerViewController: UIViewController, UITableViewDataSource {
var trip: Trip!
var valueToAppend: String?
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.
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return trip.destinations.count
}
private struct Storyboard {
static let CellReuseIdentifier = "Destination"
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath)
//configure cell
cell.textLabel?.text = trip.destinations[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated:true);
}
}
我们使用委托协议解决了应用中的相同情况。在你的情况下它看起来像这样:
定义委托协议。
protocol CategorySelectionDelegate: class {
func categorySelected(category: String)
}
在类别视图控制器 (vc1) 中定义一个委托变量。
var categorySelectionDelegate: CategorySelectionDelegate?
在您的类别视图控制器 (vc1) 中,当 table 行被选中时,触发委托,传递类别名称。
//get the category name from your model, based on the row selected...
//call the delegate, passing the category name
categorySelectionDelegate?.categorySelected(category)
将协议添加到您的主控制器 (vc2)。
class vc2: UIViewController, CategorySelectionDelegate
在您的主视图控制器 (vc2) 中,将其设置为类别视图控制器 (vc1) 的委托。
vc1.categorySelectionDelegate = self
在你的主控制器中实现协议。
func categorySelected(category: String) {
//display the items for this category in vc3
}
经过更多的研究和阅读文档,我已经解决了我的问题。以下是可能遇到类似问题的任何人解决我的问题的步骤。
1.set容器场景嵌入segue标识符
2.create 主容器视图的一个实例 VC(VC2 这个问题)
3.in main VC(在我的例子中是 VC 2)使用 prepare for segue 方法设置在 VC2.[=10 中创建的实例=]
4.set 任何可能需要设置的变量
5.make 确保从容器场景中的 table 视图创建一个 IB 出口到您的代码并将其设置为数据源。
我有一个带有分段控制元素的 UIViewController。在此之下,我有两个重叠的容器视图,它们会自动在情节提要中创建各自的视图。我可以毫无问题地将这些视图链接起来并呈现所选分段控件的每个视图。
我的问题出在我尝试在其中一个容器视图中实现 table 视图并传递数据以填充视图时。为了分解这个,假设我有 3 个视图控制器。
vc1 是 UITableViewController 类型:包含 table 个类别。
vc2 是 UI 类型ViewController:包含分段控件和两个重叠的容器视图。
vc3 的类型为 UIViewController:是具有 UITableView 的容器视图之一(我已从故事板中拖入)。此 table 显示通过 vc1 中的单元格选择的类别中的项目列表。
通常情况下,我可以使用适当的数据填充 vc3 以显示 table 的项目列表,只需使用 vc1 中的 prepareForSegue 方法即可,但由于这些之间有一个额外的 ViewController两个 ViewControllers 我没能完成相同的结果。
整体 objective 是用 vc1 中所选单元格的适当数据填充 tablevc3 中的视图。
分段视图控制器是 VC2 - 在示例中我提到了一个类别。在代码中实际上是一个包含目的地的 Trip。因此,该类别是一次旅行。类别中的项目作为旅行中的目的地(即目的地是填充 vc3 中的 table)
import UIKit
class SegmentedViewController: UIViewController {
var trip: Trip!
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var itineraryContainer: UIView!
@IBOutlet weak var plannerContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = trip.tripName
itineraryContainer.hidden = true
//Setting the initial container to be viewed to match the segmentedcontrol
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func segmentedControlAction(sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0:
plannerContainer.hidden = false
itineraryContainer.hidden = true
break
case 1:
plannerContainer.hidden = true
itineraryContainer.hidden = false
break
default:
break
}
}
}
Planner 视图控制器(容器
import UIKit
class PlannerViewController: UIViewController, UITableViewDataSource {
var trip: Trip!
var valueToAppend: String?
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.
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return trip.destinations.count
}
private struct Storyboard {
static let CellReuseIdentifier = "Destination"
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath)
//configure cell
cell.textLabel?.text = trip.destinations[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated:true);
}
}
我们使用委托协议解决了应用中的相同情况。在你的情况下它看起来像这样:
定义委托协议。
protocol CategorySelectionDelegate: class { func categorySelected(category: String) }
在类别视图控制器 (vc1) 中定义一个委托变量。
var categorySelectionDelegate: CategorySelectionDelegate?
在您的类别视图控制器 (vc1) 中,当 table 行被选中时,触发委托,传递类别名称。
//get the category name from your model, based on the row selected... //call the delegate, passing the category name categorySelectionDelegate?.categorySelected(category)
将协议添加到您的主控制器 (vc2)。
class vc2: UIViewController, CategorySelectionDelegate
在您的主视图控制器 (vc2) 中,将其设置为类别视图控制器 (vc1) 的委托。
vc1.categorySelectionDelegate = self
在你的主控制器中实现协议。
func categorySelected(category: String) { //display the items for this category in vc3 }
经过更多的研究和阅读文档,我已经解决了我的问题。以下是可能遇到类似问题的任何人解决我的问题的步骤。
1.set容器场景嵌入segue标识符
2.create 主容器视图的一个实例 VC(VC2 这个问题)
3.in main VC(在我的例子中是 VC 2)使用 prepare for segue 方法设置在 VC2.[=10 中创建的实例=]
4.set 任何可能需要设置的变量
5.make 确保从容器场景中的 table 视图创建一个 IB 出口到您的代码并将其设置为数据源。