在tableView

时间:2018-03-19 22:17:57

标签: ios swift tableview

如果标签title包含stadiumName的值,则numberOfRowsInSection函数将返回值first!.count

如果标签title包含countryName的值,则numberOfRowsInSection函数将返回值3

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

        let pfile = Bundle.main.path(forResource: "scheduling", ofType: "plist")

        let indexes = defaults.integer(forKey: "index")
        let indexes2 = defaults.integer(forKey: "index2")

        let stadiumName = stadia[indexes]
        let countryName = country[indexes2]

        let arrays = NSDictionary(contentsOfFile: pfile!)

        let first = arrays?.value(forKey: stadia[indexes]) as? [[String]]

        if (titles.text?.contains(stadiumName))!{

            let returning = first!.count

            return returning
        }
        if (titles.text?.contains(countryName))!{

            let returning = 3

            return returning
        }

    }

但是,我遇到了以下错误消息:Missing return in a function expected to return 'Int'

如果没有此错误消息,我可以做些什么来确保我可以有条件地返回值?

2 个答案:

答案 0 :(得分:2)

它说你的“if”语句并非详尽无遗,而且有些情况会在不返回值的情况下到达函数的结尾。简单的解决方案是在函数末尾添加“return 0”。

答案 1 :(得分:1)

您有两个if语句,两者都无法满足。试试这个:

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

        let pfile = Bundle.main.path(forResource: "scheduling", ofType: "plist")

        let indexes = defaults.integer(forKey: "index")
        let indexes2 = defaults.integer(forKey: "index2")

        let stadiumName = stadia[indexes]
        let countryName = country[indexes2]

        let arrays = NSDictionary(contentsOfFile: pfile!)

        let first = arrays?.value(forKey: stadia[indexes]) as? [[String]]

        if (titles.text?.contains(stadiumName))!{

            let returning = first!.count

            return returning
        }
        else if (titles.text?.contains(countryName))!{

            let returning = 3

            return returning
        } else {
            return 0
        }


    }