Swift表视图嵌入在Container中

我有一个在我的主视图中使用的NSManagedObject。

在这个视图中,我有两个容器,每个容器都有自己的静态TableViews。

在我的NSManagedObject中,我有一个我想循环的数组,并在屏幕上显示信息,如下所示:

Customer1 Name Customer1 Type Customer1 Address Customer2 Name Customer2 Type Customer2 Address 

我试图使用TableView的路线,我添加了一个容器,在其中嵌入了tableview,设置了一个自定义单元格并尝试用一些测试数据填充自定义单元格。 当我运行它时,TableView只显示四个空行。 (我可能错过了与行数量有关的事情,这就是为什么我的测试数据没有显示):

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete method implementation. // Return the number of rows in the section. return 0 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tblPartyDetails.dequeueReusableCellWithIdentifier( "JobViewPartyCell", forIndexPath: indexPath) as! JobViewPartyCell cell.lblPartyName.text = "test name" cell.lblPartyAddress.text = "test adddress" cell.lblPartyType.text = "test partyType" return cell } 

我还必须弄清楚如何将我的NSManagedObject传递到这个TableView类中,对于只是重复的信息块来说似乎需要付出很多努力……或者……这是唯一的方法吗?

那么,我是否以正确的方式解决这个问题? 如果是这样,我该如何修复它并将我的NSManagedObjects细节添加到TableView。 如果我没有正确地解决这个问题,有哪些替代方案? 我看了一些其他自定义’卡’类型的东西,比如facebook和google卡,但这些技术也使用自定义TableViewCells。

编辑。 PrepareForSeguefunction:

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == jobSegueIdentifier { if let destination = segue.destinationViewController as? JobViewController { if let jobIndex = tblJobs.indexPathForSelectedRow() { let workItem:Work = fetchedResultsController.objectAtIndexPath(jobIndex) as! Work destination.workItem = workItem } } } } 

首先,你在numberOfRowsInSection返回0,你应该做的是设置你想要显示的行数,如果你正在测试你的tableView放任何数字。

如果您的数据位于mainView中,则应将数据传递给包含的tableView,以便显示它,并且在行数中应返回数据数组中的元素数。

首先在故事板中为嵌入segue提供一个标识符,并在主视图中实现prepareForSegue函数,如下所示:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "embedSegueIdentifier" { let distinationVC = segue.destinationViewController as? EmbeddedTableViewController //replace EmbeddedTableViewController with your tableViewControllerClass distinationVC?.dataArray = yourDataArray //yourDataArray is in your main view and you should define data array in your embedded table view controller } } 

并在您的tableViewController中添加以下内容:

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataArray.count } 

我希望这有帮助。