我想将元素从数组(items2)传递到另一个数组(collCells),该数组位于不同的viewcoller中,但collCells显示的是一个空方括号
VC 1(stctView):-
var items = [Item]()
var items2 = [Item]()
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if items[indexPath.row].checked == false{
items[indexPath.row].checked = true
items2.append(items[indexPath.row])
saveData()
saveData2()
print(items2)
}else {
let remItem = items[indexPath.row]
if let index:Int = self.items2.index(where: {$0.name == remItem.name && $0.checked == remItem.checked && $0.buyPrice == remItem.buyPrice && $0.rank == remItem.rank && $0.symbol == remItem.symbol}) {
self.items2.remove(at: index)
}
saveData()
saveData2()
print(items2)
items[indexPath.row].checked = false
}
tableView.reloadData()
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Collection" {
let DVC = segue.destination as! CollectionViewController
DVC.collCells = items2
}
}
vc 2(CollectionViewController):-
class CollectionViewController: UICollectionViewController {
var collCells = [Item]()
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
collCells = stctView.items2
// Configure the cell
cell.coinName.text = collCells[indexPath.row].name
cell.buyPrice.text = "\(String(describing: collCells[indexPath.row].buyPrice))"
cell.rank.text = "\(String(describing: collCells[indexPath.row].rank))"
return cell
}
}