【问题标题】:swift: segueing in tableview sectionsswift:在 tableview 部分中进行 segue
【发布时间】:2016-01-24 00:15:31
【问题描述】:

问题:

我有一个包含 4 个部分的 tableView。在第一部分中self.performSegueWithIdentifier(String(indexPath.row), sender: self) 工作正常,它们根据indexPath.row 继续,但是一旦您向下滚动到第二、第三和第四部分并尝试点击它们,indexPath.row 就会重置。我希望能够在表格视图中一直使用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)
}

【问题讨论】:

    标签: swift uitableview segue


    【解决方案1】:

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

    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)
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多