2017-10-02 5 views
0

J'ai une UICollectionView imbriquée dans une cellule de vue de table. Comment puis-je obtenir le chemin d'index de la ligne horizontale de la cellule de vue de collection? J'ai essayé d'utiliserChemin de l'index d'une cellule de vue de collection à l'intérieur d'une cellule de vue de table

 let index = cell.tag 

     print(index as Any) 

pour imprimer l'index a été sélectionné en cours ne sachant pas qu'il imprime une valeur de zéro, peu importe ce que je sélectionne la cellule. Je suis désolé de ne pas avoir l'expérience des vues de collections. Toute aide est très appréciée. Merci.

Répondre

0

Vous devez concevoir un protocole gérant les interactions UIElement imbriquées. Il rend votre esprit plus clair et d'allouer des meilleures adresses

class ViewwithTable:UIViewController,UITableViewDataSource,collectionCell_delegate{ 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "temp", for: indexPath) as! NestedViewwithCollection 
     cell.delegate = self 
     return cell 
    } 

    func didPressed(indexPath: IndexPath) { 
     //the pressed index! 
    } 
} 

class NestedViewwithCollection:UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate{ 
    var delegate:collectionCell_delegate? 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     return UICollectionViewCell() 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     if let del = delegate{ 
      del.didPressed(indexPath: indexPath) 
     } 
    } 

} 

protocol collectionCell_delegate { 
    func didPressed(indexPath:IndexPath) 
} 
0

Utilisez ce code pour tableView

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
    return cell 
} 

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    guard let tableViewCell = cell as? TableViewCell else { return } 
    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) 
} 

Collection Voir

extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource 
{ 
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return model[collectionView.tag].count 
} 

func collectionView(collectionView: UICollectionView, 
    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
{ 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", 
     forIndexPath: indexPath) 

    cell.backgroundColor = model[collectionView.tag][indexPath.item] 

    return cell 
}