swift:在tableview部分中进行segueing

时间:2016-01-24 00:15:31

标签: swift uitableview segue

问题:

我有一个包含4个部分的tableView。在第一部分self.performSegueWithIdentifier(String(indexPath.row), sender: self)工作正常,它们根据indexPath.row进行调整,但是一旦向下滚动到第二,第三和第四部分并尝试点击它们,indexPath.row重置。我希望能够在tableview中一直使用self.performSegueWithIdentifier(String(indexPath.row), sender: self),而不会被部分重置。或者换句话说,我如何在每个部分中细分细胞。

示例:

在第一部分中说有5个单元格和第二部分。在第一部分中,5个单元格转换为正确的视图控制器,indexPath.row指向它们。一旦到第二部分,indexPath.row重置并使用与上面相同的segue。出于隐私原因,这些数组是空的。

代码:

struct Objects {
    var cellName : String!
    var sectionObjects : [String]!
    var detailObjects : [String]!
    var imageObjects : [String]!
}

var objectsArray = [Objects]()

objectsArray =
        [Objects(cellName: "example",
            sectionObjects: [],
            detailObjects: [],
            imageObjects: []),
        Objects(cellName: "example2",
            sectionObjects: [],
            detailObjects: [],
            imageObjects: []),
        Objects(cellName: "example3",
            sectionObjects: [],
            detailObjects: [],
            imageObjects: []),
        Objects(cellName: "example4",
            sectionObjects: [],
            detailObjects: [],
            imageObjects: [])]

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

    self.performSegueWithIdentifier(String(indexPath.row), sender: self)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

1 个答案:

答案 0 :(得分:1)

我发现在每个部分的segueIdentifier语句中手动设置if else解决了我的问题:

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

    if indexPath.section == 0 {
        //art
        performSegueWithIdentifier((String(indexPath.row)), sender: nil)

    } else if indexPath.section == 1 {
        //art galleries
        if indexPath.row == 0 {

            self.performSegueWithIdentifier("gallery1", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 1 {

            self.performSegueWithIdentifier("gallery2", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 2 {

            self.performSegueWithIdentifier("gallery3", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 3 {

            self.performSegueWithIdentifier("gallery4", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 4 {

            self.performSegueWithIdentifier("gallery5", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 5 {

            self.performSegueWithIdentifier("gallery6", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        }

    } else if indexPath.section == 2 {
        //historic sites
        if indexPath.row == 0 {

            self.performSegueWithIdentifier("gallery7", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 1 {

            self.performSegueWithIdentifier("gallery8", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 2 {

            self.performSegueWithIdentifier("gallery9", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 3 {

            self.performSegueWithIdentifier("gallery10", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 4 {

            self.performSegueWithIdentifier("gallery11", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 5 {

            self.performSegueWithIdentifier("gallery12", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        }

    } else if indexPath.section == 3 {
        //parks
        if indexPath.row == 0 {

            self.performSegueWithIdentifier("gallery13", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 1 {

            self.performSegueWithIdentifier("gallery14", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 2 {

            self.performSegueWithIdentifier("gallery15", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 3 {

            self.performSegueWithIdentifier("gallery16", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 4 {

            self.performSegueWithIdentifier("gallery17", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        } else if indexPath.row == 5 {

            self.performSegueWithIdentifier("gallery18", sender: self)
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        }
    }

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

}