【问题标题】:Swift table view Embedded in Container嵌入在容器中的 Swift 表格视图
【发布时间】:2015-08-14 11:02:34
【问题描述】:

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

在这个视图中,我有两个容器,每个容器都有自己的静态 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 类中,对于只是重复的信息块似乎需要付出很多努力......或者......这是唯一的方法吗?

那么,我是否以正确的方式处理这件事?如果是这样,我该如何修复它并将我的 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

                            }
        }
    }
}

【问题讨论】:

    标签: ios swift uitableview core-data


    【解决方案1】:

    首先,您在 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
    
    }
    

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的回复,我已经将对象传递给视图控制器,tableview 嵌入在该控制器中,我已经使用 prepareForSegue 函数更新了我的帖子。
    猜你喜欢
    • 2013-12-10
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    相关资源
    最近更新 更多