如何在swift 3中的不同部分下显示多维数组

时间:2017-09-20 15:55:57

标签: ios swift uitableview firebase swift3

我一直在尝试创建多维数组并在每个部分下显示每个数组..但我最终导致致命错误索引超出范围。我在下面显示的代码是我如何访问我的firebase数据并从数据库中打印每个数组。问题是我获取数据我为其分配了一个数组创建它但由于错误似乎无法显示它。希望有人帮助参考我已附加截图的错误。

@IBOutlet weak var tableview: UITableView!



var yourArray = [String]()


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




  var sundaycoursenamearray = [String]()
var sundaycoursecodearray = [String]()
var sundaycourselocationarray = [String]()
var sundayfromarray = [String]()
var sundaytoarray = [String]()

var mondaycoursenamearray = [String]()
var mondaycoursecodearray = [String]()
var mondaycourselocationarray = [String]()
var mondayfromarray = [String]()
var mondaytoarray = [String]()

var tuesdaycoursenamearray = [String]()
var tuesdaycoursecodearray = [String]()
var tuesdaycourselocationarray = [String]()
var tuesdayfromarray = [String]()
var tuesdaytoarray = [String]()


var wednesdaycoursenamearray = [String]()
var wednesdaycoursecodearray = [String]()
var wednesdaycourselocationarray = [String]()
var wednesdayfromarray = [String]()
var wednesdaytoarray = [String]()


var thursdaycoursenamearray = [String]()
var thursdaycoursecodearray = [String]()
var thursdaycourselocationarray = [String]()
var thursdayfromarray = [String]()
var thursdaytoarray = [String]()


var fridaycoursenamearray = [String]()
var fridaycoursecodearray = [String]()
var fridaycourselocationarray = [String]()
var fridayfromarray = [String]()
var fridaytoarray = [String]()

var saturdaycoursenamearray = [String]()
var saturdaycoursecodearray = [String]()
var saturdaycourselocationarray = [String]()
var saturdayfromarray = [String]()
var saturdaytoarray = [String]()

var coursecodes = [[String]]()
var coursenames = [[String]]()
var courselocations = [[String]]()
var fromtimes = [[String]]()
var totimes = [[String]]()


 var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

override func viewWillAppear(_ animated: Bool) {




    let uid = Auth.auth().currentUser?.uid
    if(uid == nil){
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let nextviewController = storyboard.instantiateViewController(withIdentifier: "loginscreen")
        //self.present(profileViewController, animated: true, completion: nil)
        self.present(nextviewController, animated: true, completion: nil)
    }


    for j in days {

    for i in 1 ..< 10 {

    let ref1 = Database.database().reference().child("users").child((uid)!).child("courses").child(j).child(String(i))
          ref1.observeSingleEvent(of: .value, with: { snapshot in

        if let dictionary = snapshot.value as? [String: AnyObject] {
           // print(dictionary)

            if j == "Sunday" {
            if let points = dictionary["coursname"] as? String {

                self.sundaycoursecodearray.append(points)
                print(self.sundaycoursecodearray)

            }

                if let points1 = dictionary["coursecode"] as? String {

                    self.sundaycoursenamearray.append(points1)
                    print(self.sundaycoursenamearray)

                }

                if let points1 = dictionary["courseroomlocation"] as? String {

                    self.sundaycourselocationarray.append(points1)
                    print(self.sundaycourselocationarray)

                }

                if let points1 = dictionary["fromtime"] as? String {

                    self.sundayfromarray.append(points1)
                    print(self.sundayfromarray)

                }

                if let points1 = dictionary["totime"] as? String {

                    self.sundaytoarray.append(points1)
                    print(self.sundaytoarray)

                }
            }

            if j == "Monday" {
                if let points = dictionary["coursname"] as? String {

                    self.mondaycoursecodearray.append(points)
                    print(self.mondaycoursecodearray)

                }

                if let points1 = dictionary["coursecode"] as? String {

                    self.mondaycoursenamearray.append(points1)
                    print(self.mondaycoursenamearray)

                }

                if let points1 = dictionary["courseroomlocation"] as? String {

                    self.mondaycourselocationarray.append(points1)
                    print(self.mondaycourselocationarray)

                }

                if let points1 = dictionary["fromtime"] as? String {

                    self.mondayfromarray.append(points1)
                    print(self.mondayfromarray)

                }

                if let points1 = dictionary["totime"] as? String {

                    self.mondaytoarray.append(points1)
                    print(self.mondaytoarray)

                }
            }

            if j == "Tuesday" {
                if let points = dictionary["coursname"] as? String {

                    self.tuesdaycoursecodearray.append(points)
                    print(self.tuesdaycoursecodearray)

                }

                if let points1 = dictionary["coursecode"] as? String {

                    self.tuesdaycoursenamearray.append(points1)
                    print(self.tuesdaycoursenamearray)

                }

                if let points1 = dictionary["courseroomlocation"] as? String {

                    self.tuesdaycourselocationarray.append(points1)
                    print(self.tuesdaycourselocationarray)

                }

                if let points1 = dictionary["fromtime"] as? String {

                    self.tuesdayfromarray.append(points1)
                    print(self.tuesdayfromarray)

                }

                if let points1 = dictionary["totime"] as? String {

                    self.tuesdaytoarray.append(points1)
                    print(self.tuesdaytoarray)

                }
            }

            if j == "Wednesday" {
                if let points = dictionary["coursname"] as? String {

                    self.wednesdaycoursecodearray.append(points)
                    print(self.wednesdaycoursecodearray)

                }

                if let points1 = dictionary["coursecode"] as? String {

                    self.wednesdaycoursenamearray.append(points1)
                    print(self.wednesdaycoursenamearray)

                }

                if let points1 = dictionary["courseroomlocation"] as? String {

                    self.wednesdaycourselocationarray.append(points1)
                    print(self.wednesdaycourselocationarray)

                }

                if let points1 = dictionary["fromtime"] as? String {

                    self.wednesdayfromarray.append(points1)
                    print(self.wednesdayfromarray)

                }

                if let points1 = dictionary["totime"] as? String {

                    self.wednesdaytoarray.append(points1)
                    print(self.wednesdaytoarray)

                }
            }

            if j == "Thursday" {
                if let points = dictionary["coursname"] as? String {

                    self.thursdaycoursecodearray.append(points)
                    print(self.thursdaycoursecodearray)

                }

                if let points1 = dictionary["coursecode"] as? String {

                    self.thursdaycoursenamearray.append(points1)
                    print(self.thursdaycoursenamearray)

                }

                if let points1 = dictionary["courseroomlocation"] as? String {

                    self.thursdaycourselocationarray.append(points1)
                    print(self.thursdaycourselocationarray)

                }

                if let points1 = dictionary["fromtime"] as? String {

                    self.thursdayfromarray.append(points1)
                    print(self.thursdayfromarray)

                }

                if let points1 = dictionary["totime"] as? String {

                    self.thursdaytoarray.append(points1)
                    print(self.thursdaytoarray)

                }
            }
            if j == "Friday" {
                if let points = dictionary["coursname"] as? String {

                    self.fridaycoursecodearray.append(points)
                    print(self.fridaycoursecodearray)

                }

                if let points1 = dictionary["coursecode"] as? String {

                    self.fridaycoursenamearray.append(points1)
                    print(self.fridaycoursenamearray)

                }

                if let points1 = dictionary["courseroomlocation"] as? String {

                    self.fridaycourselocationarray.append(points1)
                    print(self.fridaycourselocationarray)

                }

                if let points1 = dictionary["fromtime"] as? String {

                    self.fridayfromarray.append(points1)
                    print(self.fridayfromarray)

                }

                if let points1 = dictionary["totime"] as? String {

                    self.fridaytoarray.append(points1)
                    print(self.fridaytoarray)

                }
            }
            if j == "Saturday" {
                if let points = dictionary["coursname"] as? String {

                    self.saturdaycoursecodearray.append(points)
                    print(self.saturdaycoursecodearray)

                }

                if let points1 = dictionary["coursecode"] as? String {

                    self.saturdaycoursenamearray.append(points1)
                    print(self.saturdaycoursenamearray)

                }

                if let points1 = dictionary["courseroomlocation"] as? String {

                    self.saturdaycourselocationarray.append(points1)
                    print(self.saturdaycourselocationarray)

                }

                if let points1 = dictionary["fromtime"] as? String {

                    self.saturdayfromarray.append(points1)
                    print(self.saturdayfromarray)

                }

                if let points1 = dictionary["totime"] as? String {

                    self.saturdaytoarray.append(points1)
                    print(self.saturdaytoarray)

                }
            }


            self.coursecodes.append(self.sundaycoursenamearray)
            self.coursecodes.append(self.mondaycoursenamearray)
            self.coursecodes.append(self.tuesdaycoursenamearray)
            self.coursecodes.append(self.wednesdaycoursenamearray)
            self.coursecodes.append(self.thursdaycoursenamearray)
            self.coursecodes.append(self.fridaycoursenamearray)
            self.coursecodes.append(self.saturdaycoursenamearray)


            self.coursenames.append(self.sundaycoursecodearray)
            self.coursenames.append(self.mondaycoursecodearray)
            self.coursenames.append(self.tuesdaycoursecodearray)
            self.coursenames.append(self.wednesdaycoursecodearray)
            self.coursenames.append(self.thursdaycoursecodearray)
            self.coursenames.append(self.fridaycoursecodearray)
            self.coursenames.append(self.saturdaycoursecodearray)

            self.courselocations.append(self.sundaycourselocationarray)
            self.courselocations.append(self.mondaycourselocationarray)
            self.courselocations.append(self.tuesdaycourselocationarray)
            self.courselocations.append(self.wednesdaycourselocationarray)
            self.courselocations.append(self.thursdaycourselocationarray)
            self.courselocations.append(self.fridaycourselocationarray)
            self.courselocations.append(self.saturdaycourselocationarray)

            self.fromtimes.append(self.sundayfromarray)
            self.fromtimes.append(self.mondayfromarray)
            self.fromtimes.append(self.tuesdayfromarray)
            self.fromtimes.append(self.wednesdayfromarray)
            self.fromtimes.append(self.thursdayfromarray)
            self.fromtimes.append(self.fridayfromarray)
            self.fromtimes.append(self.saturdayfromarray)

            self.totimes.append(self.sundaytoarray)
            self.totimes.append(self.mondaytoarray)
            self.totimes.append(self.tuesdaytoarray)
            self.totimes.append(self.wednesdaytoarray)
            self.totimes.append(self.thursdaytoarray)
            self.totimes.append(self.fridaytoarray)
            self.totimes.append(self.saturdaytoarray)


            self.tableview.reloadData()


        }
    })

        } }

    super.viewWillAppear(animated)

}





 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return days[section]
}

 func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return days.count
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return coursenames[section].count
}


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

    // Configure the cell...

  //  cell.classcode?.text = sundaycoursenamearray[indexPath.section]

    cell.classcode?.text = coursenames[indexPath.section][indexPath.row]

    return cell
}


/*
 // Override to support conditional editing of the table view.
 override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> 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, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
 if editingStyle == .delete {
 // Delete the row from the data source
 tableView.deleteRows(at: [indexPath], with: .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, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

 }
 */

/*
 // Override to support conditional rearranging of the table view.
 override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> 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 prepare(for segue: UIStoryboardSegue, sender: Any?) {
 // Get the new view controller using segue.destinationViewController.
 // Pass the selected object to the new view controller.
 }
 */



override func viewDidLoad() {
    super.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()
}

enter image description here

1 个答案:

答案 0 :(得分:2)

您应该为数据创建模型,然后创建包含模型的数组。

您可以创建一个简单的结构:

struct Course {
  var name : String?
  var code: String?
  var location: String?
  var toTime: String?
  var fromTime: String?
}

创建模型后,您可以创建包含此类结构的数组。

对于numberOfRowsInSection,您也不必使用下标。回来:

return coursenames.count

这就是你得到错误的原因。