为数组的每个索引定义新变量

时间:2019-06-27 17:35:57

标签: swift uitableview

我正在创建一个成绩记录iOS应用程序,该应用程序需要用户输入成绩并将其存储到数组中。然后在单独的视图上,将所有等级吐出到单元格表中。我想在用户每次输入新成绩时创建一个新单元格,而不是当前使这些单元格显示为硬编码的特定单元格。

我当时在想可以创建某种for循环...但是我不知道该怎么做。

struct MyVariables {
    static var physicsExamGradesArray = [Double]()
    static var GradeArray = [Double]()
    static let classesOption = ["Choose Class", "Physics", "Math"]
    static let assignmentOption = ["Choose Type","Exam", "Quiz", "Homework"]
    static var physicsGradesArray = [Double]()
}

import UIKit

class GradeLogViewController: UIViewController {

    @IBOutlet var tableView: UITableView!

    var gradesCellsToBeDisplayed: [GradeProperties] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        gradesCellsToBeDisplayed = createArray()

    }

}

func createArray() -> [GradeProperties] {

    var grades: [GradeProperties] = []

    let grade1 = GradeProperties(assignmentClass: "Physics", assignmentType: "Exam", assignmentIndex: 1, assignmentGrade: MyVariables.physicsExamGradeArray[0])
    let grade2 = GradeProperties(assignmentClass: "Physics", assignmentType: "Exam", assignmentIndex: 2, assignmentGrade: MyVariables.physicsExamGradeArray[1])

    grades.append(grade1)
    grades.append(grade2)

    return grades
}

extension GradeLogViewController: UITableViewDataSource, UITableViewDelegate {


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return gradesCellsToBeDisplayed.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let displayedGrade = gradesCellsToBeDisplayed[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "PhysicsCell") as! PhysicsCell

        cell.setContents(grade: displayedGrade)

        return cell
    }
}

现在我已经对两个年级单元进行了硬编码...但是我希望这些单元能够根据输入的年级数自动生成...

-编辑-

用户通过UITextField输入成绩,并将其值传递到struct MyVariables下显示的变量中。

0 个答案:

没有答案