errors:无法将'Section'类型的值转换为预期的参数类型'String',并且不能将'[string]'类型的值赋给'string?'

时间:2017-12-05 10:37:32

标签: ios swift3

第一个代码负责我在链接图片中附加的第一个视图控制器,它运行得很好,直到我添加注释位,使用户能够推送到下一个视图,根据所选的yearOfStudy显示学习单元例如,通过点击图片中的FEEYear3,显示相应课程下该学习年份的单位。当我取消注释时,我收到一条错误消息:“无法将'Section'类型的值转换为预期的参数类型'String'”。我应该补充说,var部分下的'Section'被设置为结构。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, ExpandableHeaderViewDelegate{

    @IBOutlet var tableView: UITableView!

    var sections=[

        Section (courses: "Electrical Engineering",
                 yearOfStudy:["FEEYear1","FEEYear2","FEEYear3","FEEYear4","FEEYear5"],
                 expanded: false),

        Section (courses: "Civil Engineering",
                 yearOfStudy:["FCEYear1","FCEYear2","FCEYear3","FCEYear4","FCEYear5"],
                 expanded: false),


        Section (courses: "Geospatial Engineering",
                 yearOfStudy:["FGEYear1","FGEYear2","FGEYear3","FGEYear4","FGEYear5"],
                 expanded: false),

        Section (courses: "Mechanical Engineering",
                 yearOfStudy:["FMEYear1","FMEYear2","FMEYear3","FMEYear4","FMEYear5"],
                 expanded: false),

        Section (courses: "Biosystems Engineering",
                 yearOfStudy:["FBEYear1","FBEYear2","FBEYear3","FBEYear4","FBEYear5"],
                 expanded: false)
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].yearOfStudy.count
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (sections[indexPath.section].expanded){return 44}
        else{return 0}
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 2
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header=ExpandableHeaderView()
        header.customInit(title: sections[section].courses, section: section, delegate: self)
        return header
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
       cell.textLabel?.text = sections[indexPath.section].yearOfStudy[indexPath.row]
        return cell
    }

    func toggleSection(header: ExpandableHeaderView, section: Int) {
        sections[section].expanded = !sections[section].expanded
        tableView.beginUpdates()
        for i in 0..<sections[section].yearOfStudy.count{
            tableView.reloadRows(at: [IndexPath(row:i,section:section)], with: .automatic)
        }
        tableView.endUpdates()
    }

 /*   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let unitsVC = UnitsVC()
        unitsVC.customInit(sectionsIndex: indexPath.row, title: sections[indexPath.row])
        self.navigationController?.pushViewController(unitsVC, animated: true)
        tableView.deselectRow(at: indexPath, animated: true)
    }
    */
}

The image is for the first page of my project app

在第二个代码中,它具有第二个ViewController的功能,根据选择的内容相应地显示各种studyYears的单位我在写入的行上出现错误:“cell.textLabel?.text = unitsData [sectionsIndex] [indexPath.row]“ 其中说,“不能将'[string]'类型的值赋给'string?' ”。我如何摆脱这些错误?

import UIKit

class UnitsVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!


    let unitsData = [
        [["fee101","fee111","fee121","fee131","fee141","fee151","fee161","fee102","fee112","fee122","fee132","fee142","fee152","fee162"],["fee201","fee221","fee231","fee241","fee251","fee261","fee271","fee281","fee202","fee222","fee232","fee242","fee252","fee262","fee272","fee282"],["fee301","fee321","fee331","fee341","fee351","fee361","fee371","fee381","fee302","fee322","fee332","fee342","fee352","fee362","fee372","fee382"],["fee401","fee411","fee421","fee431","fee441","fee451","fee471","fee481","fee402","fee412","fee422","fee432","fee442","fee452","fee472","fee482"],["fee501","fee511","fee521","fee531","fee541","fee551","fee560","fee571","fee591","fee502","fee512","fee522","fee532","fee542","fee552","fee560","fee582","fee592"]],

        [["fce101","fce131","fce161","fce163","fce165","fce181","fce103","fce132","fce162","fce164","fce166","fce182"],
         ["fce201","fce231","fce211","fce245","fce251","fce261","fce265","fce232","fce242","fce222","fce246","fce262","fce272"],
         ["fce301","fce311","fce321","fce331","fce341","fce351","fce361","fce302","fce312","fce322","fce332","fce342","fce352","fce362","fce392"],
         ["fce401","fce411","fce421","fce425","fce431","fce451","fce461","fce481","fce402","fce412","fce422","fce426","fce432","fce446","fce472"],
         ["fce581","fce511","fce525","fce531","fce545","fce571","fce564","fce590","fce512","fce526","fce532","fce572","fce552"]],

        [["fme169","fme111","fme151","fme161","fme171","fme173","fme112","fme152","fme165","fme166","fme167","fme168","fme172","fme174","fme182"],["fme201","fme211","fme251","fme261","fme271","fme281","fme291","fme202","fme212","fme222","fme244","fme252","fme262","fme272"],["fme301","fme311","fme321","fme331","fme343","fme351","fme371","fme363","fme391","fme302","fme312","fme322","fme332","fme344","fme352","fme362","fme372"],["fme411","fme431","fme432","fme452","fme471","fme472","fme492","fme401","fme421","fme422","fme441","fme442","fme461","fme462"],["fme511","fme521","fme531","fme541","fme551","fme561","fme581","fme502","fme503","fme504","fme513","fme523","fme524","fme525","fme533","fme524","fme542","fme543","fme544","fme545","fme552"]]

    ]

    var sectionsIndex: Int!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.dataSource = self
        self.tableView.delegate = self

        let nib = UINib(nibName:"UnitsCell", bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: "unitsCell")

        // Do any additional setup after loading the view.
    }

    func customInit(sectionsIndex:Int, title: String ) {

        self.sectionsIndex=sectionsIndex
        self.title=title//attribute for navigation controller bar
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return unitsData[sectionsIndex].count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "unitsCell", for: indexPath) as! UnitsCell
       cell.textLabel?.text = unitsData[sectionsIndex][indexPath.row]//line with error
        return cell

    }

1 个答案:

答案 0 :(得分:0)

看起来你有一个数组数组。因此,根据您的图像,假设您想从电气工程中选择第3年。

(我假设您使用TableView Sections获取此答案,因为您没有提供其他代码。)

要获得第3年,您只需访问let year3Data = unitsData[0][2]