ios - 在 View 之间传递数组值 [swift 3] [iOS]

标签 ios arrays iphone swift

我一直在使用 swift 3 开发应用程序,
这里有点新(快速编程),
所以事情是这样的,
我想将数据从第一个 TableView Controller 中的数组传递到第二个 TableView Controller ,然后在第二个 TableView Controller 中我想使用 if 函数根据我一直在使用的值在 TableView 中显示不同的图像在第一个 TableView 中,
我已经做了 segue 的事情,但没有成功
这是我的第一个 TableView Controller 代码块

var FirstTableArray = [String]()
var SecondArray = [SecondTable]()

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    FirstTableArray = ["Alam","Sejarah dan Budaya","Buatan"]

    SecondArray = [SecondTable(SecondTitle:["11","12","13"]),
                   SecondTable(SecondTitle:["21","22","23"]),
                   SecondTable(SecondTitle:["31","32","33"])]
}

“FirstTableArray”是我想要传递其值的数组,这是第一个 TableView Controller 中的其他代码片段,用于使用“FirstTableArray”值在 TableView 中显示标签

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let Cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CostumTableViewCell

    Cell.judulGambar.text = FirstTableArray[indexPath.row]
    Cell.namaGambar.image = UIImage(named: gambarMenu[indexPath.row])

    Cell.namaGambar.layer.cornerRadius = Cell.namaGambar.frame.size.width/2
    Cell.namaGambar.clipsToBounds = true

    return Cell
}

现在我已经使用此准备segue方法成功传递了“SecondArray”值,但我无法对“FirstTableArray”值执行此操作 这是我的转接方法

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var IndexPath : IndexPath = self.tableView.indexPathForSelectedRow!
    let DestViewController = segue.destination as! SecondTableViewController

    var SecondArrayTableTwo : SecondTable

    SecondArrayTableTwo = SecondArray[IndexPath.row]
    DestViewController.SecondArray = SecondArrayTableTwo.SecondTitle

}

现在这是我的第二个 TableView Controller ,我想将第一个 TableView Controller 数据传递到此 View ,我的第二个数组数据已传递其值,但我无法对“FirstTableArray”值执行此操作 我的第二个 TableView Controller 代码

   var namaGambarDetail = ["gunungmahawu.jpg","pantaimalalayang.jpg","pulaumanadotua.jpg","pulausuladen.jpg"]
var gambarSejarah = ["pantaimalalayang.jpg","gunungmahawu.jpg","pulaumanadotua.jpg","pulausuladen.jpg"]

var FirstArray = [String]()
var SecondArray = [String]()


override func viewDidLoad() {
    super.viewDidLoad()
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let Cell = self.tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! CostumTableViewCell

    Cell.judulDetail.text = SecondArray[indexPath.row]

    Cell.gambarTampilDetail.image = UIImage(named: namaGambarDetail[indexPath.row])

    Cell.gambarTampilDetail.layer.cornerRadius = Cell.gambarTampilDetail.frame.size.width/2
    Cell.gambarTampilDetail.clipsToBounds = true



    return Cell
}

我想在单击该行时传递“FirstTableArray”值数据,例如所选值的索引路径,这样我就可以使用这样的 if 语句函数根据第一个 View 中单击的值更改图像

if(FirstTableArray.text.contains("my first table view array values")){ Cell.gambarTampilDetail.image = UIImage(named: namaGambarDetail[indexPath.row])}

抱歉我的帖子很长,希望你们明白我的意思,抱歉我的英语不好,cmiiw,谢谢,感谢任何帮助:)

最佳答案

从 SecondTableViewController 中删除此行

var SecondArray = [String]()

关于ios - 在 View 之间传递数组值 [swift 3] [iOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43834884/

相关文章:

ios - 如何从MasterView的TableViewCell(Xib Cell)导航到DetailView的TableView

android - 在 Flutter 中改变方向时保留 TextField 值

ruby - 迭代字符串数组并执行正则表达式

javascript - 如何从同一数组的反向查找数组 1 的相邻元素

ios - 是否有可能在 ios 上获得相机的焦距?

iphone - 何时释放/保留传递给辅助线程的对象?

ios - 使用 AFNetworking 上传大文件?

iphone - 在 iOS 中更改 kCTLigatureAttributeName 的值

arrays - ReDim 数组但保留值

iphone - MapKit 内存泄漏。不知道为什么