gpt4 book ai didi

swift - 在 Swift 中的 TableView 之间传递数据

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:52 24 4
gpt4 key购买 nike

我正在尝试将数据从主 TableView Controller 传递到详细 TableView Controller 。我的详细信息中有很多行,上面标有“标签”。我想我在指定一个部分中的行数时犯了一些错误,但不确定如何修复它。任何帮助将非常感激。提前致谢

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

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

这是我的详细 TableView

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

最佳答案

你的问题出在这里:

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

您已分配:

subsectionViewController.sectionProperties = arrayOfSections

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

关于swift - 在 Swift 中的 TableView 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com