代码之家  ›  专栏  ›  技术社区  ›  Abhinav Khanduja

为什么数组(collCells)在swift中从不同的ViewController传递数组元素时显示为空?

  •  0
  • Abhinav Khanduja  · 技术社区  · 6 年前

    我想将元素从数组(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
       }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   pankaj    6 年前

    您的prepare for segue功能看起来很旧,请按以下方式使用它:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    }