简体   繁体   English

TableView,数组和序列

[英]TableView, Array and segue

I am a beginner in swift this actually my second week . 我实际上是我的第二个星期。 I try to teach my self by building simple code. 我试图通过构建简单的代码来教自己。

I build a table using class and array, 我使用类和数组构建表,

    var cell = [Cell]()

    override func viewDidLoad() {
        super.viewDidLoad()
        cell = [
            Cell(Title:"cell1", number: 5),
            Cell(Title:"cell2",  number: 3),
            Cell(Title:"cell3", number: 4),
]
        // Do any additional setup after loading the view, typically from a nib.
           }

If I click one of the cells I will move to next controller. 如果单击其中一个单元格,则将移至下一个控制器。 Here the idea, all the cell will take me to the same controller the only difference is the number of the cells so I want to pass the data 在这里,所有单元将把我带到同一个控制器,唯一的区别是单元的数量,所以我想传递数据

cell[row].number cell [row] .number

to next controller 到下一个控制器

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "next" {
        if let indexPath = tableView.indexPathForSelectedRow {
           let selectedcell = cell[indexPath.row]
            let dest = (segue.destination as! UINavigationController).topViewController as! DetailViewController

            dest.cells = selectedcell


        }}

My problem is I wasnt able to use cells in DetailViewController maybe their is code for pass the data of array or something. 我的问题是我无法使用DetailViewController中的单元格,也许它们是用于传递数组数据或某些内容的代码。 I want to use cells.number as the number of cells when I build the table. 我要在构建表时使用cells.number作为单元格数。 I know there simple way by making each cells points to different Controller but if I have big data it will take forever. 我知道使每个单元指向不同的Controller的简单方法,但是如果我有大数据,那将是永远的。

thank you 谢谢

I guess the issue is with 我想问题出在

dest.cells = selectedcell

You are trying to pass 1 Cell object to a variable containing (i suppose, from the plural) an array of Cell-s. 您正在尝试将1个Cell对象传递给包含(我想是复数的)Cell-s数组的变量。

  • If you just want to pass 1 cell just correct the destination type to Cell . 如果只想传递1个单元格,则将目标类型更正为Cell

  • If you want to pass an array of cells then you need to tell us with which policy 如果您想传递一组单元格,则需要告诉我们哪种政策

  • If you want to instantiate in the destination view controller as many new cells as the number defined by the selected cell.number then pass the cell as you are doing and in the destination view controller implement the needed uitableviewdelegate/datasource and instantiate them. 如果要在目标视图控制器中实例化与所选cell.number定义的数量一样多的新单元格, cell.number传递该单元格,然后在目标视图控制器中实现所需的uitableviewdelegate / datasource并实例化它们。

[Should be a comment but is too long] [应该是评论,但时间太长]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM