【问题标题】:Passing Data between Table View in Swift在 Swift 中的表视图之间传递数据
【发布时间】:2014-10-18 12:02:36
【问题描述】:

我正在尝试将数据从主表视图控制器传递到详细表视图控制器。我的详细信息中有很多行,上面标有“标签”。我认为我在指定部分中的行数时犯了一些错误,但不知道如何解决它。任何帮助将非常感激。提前致谢

这就是我将数据从主表视图传递到详细表视图的方式

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

【问题讨论】:

  • 请贴出data pass的代码和detail view controller的数据源。
  • 至少从主表视图控制器发布您的 prepareForSegue() 函数。
  • 刚刚添加了更多细节

标签: swift tableview


【解决方案1】:

你的问题在这里:

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

您已分配:

subsectionViewController.sectionProperties = arrayOfSections

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

【讨论】:

  • 谢谢史蒂夫,我在主 VC 中定义的数据源有点不同。我现在已经用数据源编辑了我的问题。我注意到我有两个问题。 1) 主表视图 - 显示 sectionHeader 字符串,但需要根据相同的字符串对其进行分组 2) 当我点击主 vc 中的一个单元格时,我可以看到详细视图上显示的过滤部分项目...说字符串“小节
  • 不确定我是否理解。您能否具体说明什么显示不正确与应该显示什么?
  • 如果你看到我的数据,我有 11 行数据。所以我得到11行。如果您查看 sectionHeader,我有 8 个不同的部分,例如“等角、不等角、通用梁、通用柱、平行面法兰……矩形空心截面。我只想显示 8 个而不是 11 个,即不想显示再次显示相同的项目。当我点击主 VC 中的行时,我也得到 11 行。例如,我单击“等角”...我得到 3 行的值,因为我有 3 个等角,其余 8 行是显示值“标签”。我只想要3个出现。任何帮助或建议将不胜感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
相关资源
最近更新 更多