调用特定的表部分和行以进行替代搜索? (以编程方式)

时间:2019-05-17 16:16:11

标签: swift uitableview

使用动态单元格调用特定的表节和行时遇到问题。该表中嵌入了一个导航控制器,现有单元格使用名为“ detailSegue”到“ LibraryViewController”的序列。我想将特定的单元格部分和行重定向到名称为“ webSegue”到“ WebViewController”的segue的备用视图控制器。

这是我当前的代码:

class LibraryTableViewController: UITableViewController {

    var selectedFileName = ""

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

    var objectArray = [Objects]()

    override func viewDidLoad() {
        super.viewDidLoad()

        objectArray = [Objects(sectionName: "Led Zeppelin", sectionObjects: ["Houses Of The Holy", "Physical Graffiti", "Presence"], sectionImages: ["Houses Of The Holy", "Physical Graffiti", "Presence"]),
                       Objects(sectionName: "Dave Matthews Band", sectionObjects: ["Crash", "Before These Crowded Streets", "Everyday"], sectionImages: ["Crash", "Before These Crowded Streets", "Everyday"]),
                       Objects(sectionName: "Drake", sectionObjects: ["Scorpion", "Views", "So Far Gone"], sectionImages: ["Scorpion", "Views", "So Far Gone"]),
                       Objects(sectionName: "Jay-Z", sectionObjects: ["The Black Album", "American Gangster", "The Blueprint"], sectionImages: ["The Black Album", "American Gangster", "The Blueprint"]),
                       Objects(sectionName: "Others", sectionObjects: ["Other Albums", "User Guide"], sectionImages: ["Other Albums", "User Guide"])
        ]
    }

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

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "libraryCell", for: indexPath)
        cell.textLabel?.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
        cell.imageView?.image = UIImage(named: objectArray[indexPath.section].sectionImages[indexPath.row])
        return cell
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return objectArray[section].sectionName
    }


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedFileName = objectArray[indexPath.section].sectionObjects[indexPath.row]
        if indexPath.section == 4 && indexPath.row == 1
        {
            performSegue(withIdentifier: "webSegue", sender: self)
        }
        else
        {
        performSegue(withIdentifier: "detailSegue", sender: self)
        }
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        let objLibraryViewController = segue.destination as! LibraryViewController
        objLibraryViewController.fileName = selectedFileName
        }

}

我在'didSelectRow'中建立了'if'语句;

if indexPath.section == 4 && indexPath.row == 1
        {
            performSegue(withIdentifier: "webSegue", sender: self)
        }
        else
        {
        performSegue(withIdentifier: "detailSegue", sender: self)
        }

但是,我正在调用的特定单元格仍使用将我带到LibraryViewController的'detailSegue'。

我需要重定向的唯一单元格是“其他相册”单元格,恰好是第4部分和第1行。

我遗漏了什么导致对WebViewController的选择被忽略?

1 个答案:

答案 0 :(得分:0)

您还必须在prepare(for中区分segue,执行 web segue无需更改。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "detailSegue" {
        let objLibraryViewController = segue.destination as! LibraryViewController
        objLibraryViewController.fileName = selectedFileName
    }
}