【发布时间】:2017-05-07 18:03:43
【问题描述】:
我一直在 swift 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”是我想传递其值的数组,这是第一个表视图控制器中的其他代码 sn-p,用于使用“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,谢谢,感谢任何帮助:)
【问题讨论】: