在Swift的表视图控制器中未正确显示表部分

时间:2016-04-02 00:18:44

标签: ios swift

我在TableViewController上的Storyboard中声明了两个部分。在相应的快速课程中,我有:

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

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

因为我有2个部分和6个行。但是,这给了我连续的Index out of bounds错误,这很烦人,因为这些部分正在被声明!我做错了什么?

提前致谢。

更新

好的,这是我的表视图控制器:

enter image description here

我在cellForRow中写的是......

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Identifier", forIndexPath: indexPath)

    // Configure the cell...
    cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"

    return cell


}

我是Swift的新手,所以我不确定这是否正确,但基本上我得到了(注释掉了cellForRow只是第一部分。)

这有帮助吗?

3 个答案:

答案 0 :(得分:0)

如果每个部分有不同的行数需要将它们分开,则无法返回总数:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section { 
    case 0: return 5 
    case 1: return 1 
    default: return 0 
    }
}

在检查器窗口中,如果您有静态内容,请尝试将其设置为动态,您只需要一个单元格原型。

enter image description here

答案 1 :(得分:0)

<强>答案

大家好,以防万一有人对答案感兴趣:

在Swift中,如果您使用的是静态单元格,那么您并不打算使用numberOfSectionsInTableView或numberOfRowsInSection,因为如果您使用的是数据源,则需要 。如果要使用静态单元格,请务必删除这两种方法。

来源:https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2

希望这有帮助!

答案 2 :(得分:0)

以下是我如何在多个部分设置表格视图Smith多行:

1)在ViewController类的顶部,创建此结构并在其下面创建变量

struct Objects {
    var sectionName : String!
    var sectionObjects: [String]!
}

var objectsArray = [Objects]()

2)在ViewDidLoad中,设置变量

objectsArray = [Objects(sectionName: "Find People", sectionObjects: ["Facebook", "Twitter", "Contacts"]), Objects(sectionName: "Your Account", sectionObjects: ["Edit Info", "Delete Account"]), Objects(sectionName: "Support", sectionObjects: ["Help", "Report a Problem", "Contact Us"]), Objects(sectionName: "App Info", sectionObjects: ["About", "Open Source Libraries", "Privacy Policy",  "Our Twitter", "Our Facebook"])]

3)对于numberOfSectionsInTableView

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return objectsArray.count

}

4)对于numberOfRowsInSection

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

    return objectsArray[section].sectionObjects.count

}

5)对于cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]

    return cell
}

6)用于设置titleForHeaderInSection

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

    return objectsArray[section].sectionName

}

7)为了在用户点击单元格时发生任何操作,您必须调用didSelectRowAtIndexPath - &gt;在我的情况下,我有4个部分(标题),所以我必须创建4个开关(案例从0开始,而不是1)。然后,对于每个部分,我必须为每个部分中的每一行设置一个案例。在这种情况下,你可以调用你想要的任何东西 - 例如,你可以执行一个segue到另一个viewController。默认情况下,实际上是指if else语句中的else ...如果所有其他方法都失败,则向用户显示错误,表明某些内容无法正常工作。你可以使用内置的alertView,但我喜欢在GitHub上使用SCLAlertView!

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if reachabilityStatus == kNOTREACHABLE {

        self.displayError("No Internet Connection", message: "Please connect to the internet before continuing")


    } else {

        switch(indexPath.section) {

        case 0:


            switch(indexPath.row) {

            case 0:

                // Find People through facebook -> test below

                print("Connect with FB")

                return


            case 1:

                // Find People through twitter -> test below

                print("Connect with Twitter")

                return

            case 2:

                // Find People through contacts -> test below

                print("Connect with Contacts")


                return

            default:

                self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")

                return

            }

        case 1:

            switch(indexPath.row) {


            case 0:

                // Edit Account -> test below

                self.performSegueWithIdentifier("editProfileSegue", sender: self)

                return


            case 1:

                // Delete Account -> test below

                print("Delete Account")

                return


            default:

                self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")

                return



            }


        case 2:


            switch(indexPath.row) {

            case 0:

                // Help -> test below

                print("Help")

                return


            case 1:

                // Report a Problem -> test below

                print("Report Problem")

                return

            case 2:

                // Contact Us -> test below

                print("Contact Us")


                return

            default:

                self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")

                return

            }


        case 3:


            switch(indexPath.row) {

            case 0:

                // About App -> test below

                print("About App")

                return


            case 1:

                // Open Source Libraries -> test below

                print("Open Source Libraries")

                return

            case 2:

                // Privacy Policy -> test below

                print("Privacy Policy")


                return

            case 3:

                // Our Twitter -> Test Below

                print("Our Twitter")

            case 4:

                // Our Facebook -> Test Below

                print("Our Facebook")


                return

            default:

               self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")

                return

            }

        default:

            self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")

            print("last error")

            return


        } // close section switch


    }

}

在您的情况下,您只有3个部分,因此只需使用代码来查看有效的部分

如果您需要任何帮助,请与我联系!