简体   繁体   中英

Type Any don't have member count, two dimensional array

hi I want to make a view like this picture below, so what I try to do is to make a two dimensional array with UITableViewCell and a string. Since it types Any the compiler complain "Type Any don't have a member of count and subscript". is it my approach right? to make this view like the picture below, or should I create one by one?

表视图组

This is my code

var tableItems = [
        [ProgressCell.self],
        ["Information", "Address", "Password"],
        ["Help", "Term and Condition", "Privacy"],
        [SignOutCell]
    ] as Any

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return tableItems[section].count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)

    let name                = tableItems[indexPath.section][indexPath.row] as! Int

    cell.textLabel?.text    = name
    cell.textLabel?.font    = UIFont(name: "NunitoSans-Regular", size: 16)
    cell.accessoryType      = .disclosureIndicator

    return cell
}

I believe you want to define an array that contains Any item, which would look like this [[Any]]

Update your code to the following:

var tableItems = [
    [ProgressCell.self],
    ["Information", "Address", "Password"],
    ["Help", "Term and Condition", "Privacy"],
    [SignOutCell]
] as [[Any]]

As suggested by @fogmeister in the comment below you shouldn't be really be using Any and that further work needs to be done to define the types correctly.

The following code worked for me. Moreover, self was also needed for SignOutCell. Please see the attached link as well.

var tableItems = [
    [ProgressCell.self],
    ["Information", "Address", "Password"],
    ["Help", "Term and Condition", "Privacy"],
    [SignOutCell.self] as [Any]
  ]

https://i.stack.imgur.com/QexjG.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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