2017-10-02 8 views
0

UICollectionView가 테이블 뷰 셀 안에 중첩되어 있습니다. Collectionview 셀의 가로 행의 인덱스 경로는 어떻게 얻을 수 있습니까? 나는 그것을 상관없이 내가 선택한 어떤 셀에 0의 값을 출력 없다는 것을 모르고 선택되는 한 인덱스 인쇄테이블 뷰 셀 내부에있는 컬렉션 뷰 셀의 인덱스 경로

 let index = cell.tag 

     print(index as Any) 

을 사용했습니다. 나는 수집에 대한 경험이 없다고 사과한다. 어떤 도움이라도 대단히 감사합니다. 감사.

답변

0

중첩 된 UIElement 상호 작용을 처리하는 프로토콜을 디자인해야합니다. 그것은 더 나은 주소

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

사용있는 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) 
} 

컬렉션보기

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 
}