简体   繁体   中英

Swift xcode error: “Row.Type has no subscript members”

I am trying to create a table that has 12 rows and when you click on a row it transports the user to a new window with a list of buttons.

Screenshot of error: enter image description here

Class where I got the error:

import UIKit

class RowTable
{


    func viewDidLoad() {
       viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    func didReceiveMemoryWarning() {
       didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 12
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("FileSlot", forIndexPath: indexPath)
        2
        cell.textLabel?.text = Row[indexPath.row]["FileName"] //this is where i got the error that said Row.Type has no subscript members

        let enabledSwitch = UISwitch(frame: CGRectZero) as UISwitch
        enabledSwitch.on = true
        enabledSwitch.addTarget(self, action: "switch1:", forControlEvents: UIControlEvents.ValueChanged)
        enabledSwitch.tag = indexPath.row
        cell.accessoryView = enabledSwitch
        return cell
    }



    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Row class:

import UIKit


let rows = [
    Row(FileName: "File slot 1",
        QuartzImage: "Image slot 1"),
    Row(FileName: "File slot 2",
        QuartzImage: "Image slot 2"),
    Row(FileName: "File slot 3",
        QuartzImage: "Image slot 3"),
    Row(FileName: "File slot 4",
        QuartzImage: "Image slot 4"),
    Row(FileName: "File slot 5",
        QuartzImage: "Image slot 5"),
    Row(FileName: "File slot 6",
        QuartzImage: "Image slot 6"),
    Row(FileName: "File slot 7",
        QuartzImage: "Image slot 7"),
    Row(FileName: "File slot 8",
        QuartzImage: "Image slot 8"),
    Row(FileName: "File slot 9",
        QuartzImage: "Image slot 9"),
    Row(FileName: "File slot 10",
        QuartzImage: "Image slot 10"),
    Row(FileName: "File slot 11",
        QuartzImage: "Image slot 11"),
    Row(FileName: "File slot 12",
        QuartzImage: "Image slot 12")]

class Row
{
    ///// enum Type: String {
    // }

    var FileName: String
    var QuartzImage: String

    // var type: Type
    // var shortDescription: String
    // var longDescription: String

    init(FileName: String, QuartzImage: String) {
        self.FileName = FileName
        self.QuartzImage = QuartzImage

}
}

use: let filename = rows[indexPath.row].FileName as opposed to accessing via a key.

Also, generally properties are declared camel case starting lower, ie: fileName and quartzImage

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