ios - 嵌入在容器中的 Swift TableView

标签 ios swift uitableview core-data

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

在这个 View 中,我有两个容器,每个容器都有自己的静态 TableView。

在我的 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 类中,对于重复的信息 block 似乎需要付出很多努力......或者......这是唯一的方法吗?

那么,我的处理方式是否正确?如果是这样,我该如何修复它并将我的 NSManagedObjects 详细信息添加到 TableView。如果我没有正确地解决这个问题,有什么选择?我查看了其他一些自定义“卡片”类型的东西,例如 facebook 和 google 卡片,但这些技术也使用自定义 TableViewCells。

编辑。 PrepareForSegue 函数:

 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,以便您可以显示它,并且在您的行数中,您应该返回数据数组中的元素数。

首先在 Storyboard中为您的嵌入 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

}

希望对您有所帮助。

关于ios - 嵌入在容器中的 Swift TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32008516/

相关文章:

ios - Xamarin.iOS - 如何动态更改标签字体大小?

ios - 使用 Swift 3 解析 JSON

swift - 将表格 View 设置为编辑模式

ios - CollectionViewCell 中的 Tableview

c# - MonoTouch 对话框的 UITableView。如何处理水龙头?

ios - XCode 4.2 Lion(iOS 产品)中与 Objective-C++/C++ 的链接错误

ios - 有没有办法从 React Native 中的 chrome 控制台重新加载设备,这样我就不必不断地摇动它?

ios - iOS上的Accelerate框架可以用来画圆吗?

ios - 在一个按钮中使用 segue 和 IBAction

ios - 如何为作为 UITableView 页脚放置的 UIView 设置固定高度?