2014-10-18 41 views
2

我想从主表视图控制器传递数据到详细的tableview控制器。在我的详细信息中,我收到很多行,并标注了“标签”。我认为我在指定某一部分的行数时出错,但不知道如何解决该问题。任何帮助将非常感激。在此先感谢Swift中的表视图之间传递数据

这是我如何通过从主表视图中的数据细节表视图

import UIKit 

class mainSectionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{ 

    @IBOutlet weak var mainSectionTableView: UITableView! 

    var arrayOfSections : [Sections] = [Sections]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.setupmainSections() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 

    func setupmainSections() { 

     var EA503 = Sections (sectionHeader: "Equal Angle", sectionImage: "angle equal.gif", subSection: "50x50x5") 

     var EA756 = Sections (sectionHeader: "Equal Angle", sectionImage: "angle equal.gif", subSection: "75x75x6") 

     var UA50403 = Sections (sectionHeader: "Unequal Angle", sectionImage: "angle unequal.gif", subSection: "75x75x6") 

     var UA90608 = Sections (sectionHeader: "Unequal Angle", sectionImage: "angle unequal.gif",subSection: "90x60x8") 

     var UC10015 = Sections (sectionHeader: "Universal Column", sectionImage: "Column.gif", subSection: "UC10015") 

     var UB15015 = Sections (sectionHeader: "Universal Beam", sectionImage: "beam.gif",subSection: "UB15015") 

     var PFC100 = Sections (sectionHeader: "Parallel Flange Chanel", sectionImage: "PFC.gif", subSection: "PFC100") 

     var CHS300 = Sections (sectionHeader: "Circular Hollow Section", sectionImage: "chs.gif", subSection: "CHS300") 


     var SHS200 = Sections (sectionHeader: "Square Hollow Section", sectionImage: "shs.gif",subSection: "SHS200") 

     var RHS50 = Sections (sectionHeader: "Rectangular Hollow Section", sectionImage: "rhs.gif", subSection: "RHS50") 

     var EA253 = Sections (sectionHeader: "Equal Angle", sectionImage: "angle equal.gif", subSection: "25x25x3") 

     arrayOfSections.append(EA503) 
     arrayOfSections.append(EA756) 
     arrayOfSections.append(UA50403) 
     arrayOfSections.append(UA90608) 
     arrayOfSections.append(UC10015) 
     arrayOfSections.append(UB15015) 
     arrayOfSections.append(PFC100) 
     arrayOfSections.append(CHS300) 
     arrayOfSections.append(SHS200) 
     arrayOfSections.append(RHS50) 
     arrayOfSections.append(EA253) 
    } 

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

     let mainsection = arrayOfSections[indexPath.row] 
     let subsectionViewController : detailSectionViewController = self.storyboard?.instantiateViewControllerWithIdentifier("detailSectionViewController") as detailSectionViewController 

     subsectionViewController.headerLabelText = mainsection.sectionheader 
     subsectionViewController.sectionProperties = arrayOfSections 

     presentViewController(subsectionViewController, animated: true, completion: nil) 
    } 
} 

这是我的详细信息表视图

import UIKit 

class detailSectionViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate { 

    @IBOutlet weak var headerLabel: UILabel! 

    @IBOutlet weak var subSectionTableView: UITableView! 

    var headerLabelText : String? 
    var sectionProperties : [Sections] = [Sections]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.headerLabel.text = headerLabelText 
     headerLabel.backgroundColor = UIColor.lightGrayColor() 
     headerLabel.layer.cornerRadius = 10.0 
     headerLabel.clipsToBounds = true 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 

    override func prefersStatusBarHidden() -> Bool { 
     return true 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return sectionProperties.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let subCell : detailSectionViewCell = tableView.dequeueReusableCellWithIdentifier("subCell") as detailSectionViewCell 

     let subsection = sectionProperties[indexPath.row] 

     if (subsection.sectionheader == headerLabelText) { 

      subCell.setupSubCell(subsection.subsection) 

     } 

     return subCell 
    } 
} 
+0

请发布数据传递的代码和详细视图控制器的数据源。 – 2014-10-18 12:09:27

+0

至少从主表视图控制器发布您的prepareForSegue()函数。 – 2014-10-18 12:15:19

+0

刚刚添加更多详细信息 – Magesh 2014-10-18 12:29:12

回答

0

你的问题就在这里:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return sectionProperties.count 
    } 

已转让:

subsectionViewController.sectionProperties = arrayOfSections

那么,arrayOfSections有11个成员。 arrayOfSections.sectionHeader有8个唯一成员(但总共11个条目)。您将声明一个参数来计算唯一的sectionHeaders的数量。

+0

谢谢Steve,我在主VC中定义的数据源有点不同。我现在用数据源编辑了我的问题。我注意到我有两个问题。 1)主表视图 - 显示sectionHeader字符串,但需要根据相同的字符串进行分组2)当我点击一个单元格在主vc中,我可以看到过滤部分显示在详细视图中的项目...说字符串“小节 – Magesh 2014-10-18 22:13:14

+0

不知道我明白了,你能具体说明什么显示不正确与显示什么吗? – 2014-10-19 00:10:06

+0

如果你看到我的数据我有11行数据,所以我得到11行,如果你看看sectionHeader I有“等角,不等角,通用梁,通用柱,平行面法兰,...矩形空心截面等8种不同的形状。我只想显示8而不是11,即不想再次显示相同的项目。当我点击主VC中的行时,我也获得了11行。例如,我点击“等角”...我得到3行的值,因为我有3个等角,剩下的8行显示值“label”。我只想出现3个。任何帮助或建议将不胜感激 – Magesh 2014-10-19 01:05:25

相关问题