簡體   English   中英

在視圖之間傳遞數組值[swift 3] [iOS]

[英]passing array values between view [swift 3] [iOS]

我一直在迅速開發應用程序3,
這里有點新(快速編程),
就是這樣
我想將數據從第一個表視圖控制器中的數組傳遞給第二個表視圖控制器,然后在第二個表視圖控制器中,我想使用if函數根據我一直在使用的值在表視圖中顯示不同的圖像在第一個表格視圖中
我已經做過segue了,但是沒用
這是我的第一個表格視圖控制器代碼塊

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”是我想要傳遞其值的數組,這是第一個表視圖控制器中的其他代碼段,用於使用“ FirstTableArray”值在表視圖中顯示標簽

    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”值做到這一點,這是我的segue方法

    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

}

現在這是我的第二個表視圖控制器,我想將第一個表視圖控制器數據傳遞給該視圖,我的第二個數組數據已經傳遞了它的值,但是我無法為我的第二個表視圖控制器代碼的“ FirstTableArray”值執行此操作

   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語句功能在基於第一個視圖的單擊值基礎上更改圖像

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

對不起,我的長篇文章,希望你們能理解我的意思,對不起我的英語水平,cmiiw,謝謝,感謝您的幫助:)

從SecondTableViewController刪除此行

var SecondArray = [String]()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM