如何在tableview里面的collectionview ios swift中获取所选索引

时间:2016-04-05 05:23:39

标签: uitableview uicollectionview uicollectionviewcell

UITableView->表格视图单元格 - >集合视图 - >集合视图单元格。

当我单击集合视图单元格时,只获取集合视图的选定索引cell.how以获取表视图中的索引。

感谢。

2 个答案:

答案 0 :(得分:1)

如果你想要另一个视图,你可以在你的UIViewController中使用prepare to segue并获取tableView索引

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "segueHomeToDetail" {
            if let collectionCell: InfoCellView = sender as? InfoCellView {
                if let collectionView: UICollectionView = collectionCell.superview as? UICollectionView {
                    if let destination = segue.destinationViewController as? DetailViewController {
                        // Pass some data to YourViewController
                        // collectionView.tag will give your selected tableView index
                        destination.object = collectionCell.object

                    }
                }
            }
        }

    }

我在我的代码上使用它并且工作正常。

答案 1 :(得分:1)

实现ScrollView方法以获取Tableview和集合视图之外的索引值。

 if scrollView == tableview {
            let contentOffset = scrollView.contentOffset.y

            print("contentOffset: ", contentOffset)
            if (contentOffset > scrollView.contentOffset.y) {
                print("scrolling Down")

            } else {
                print("scrolling Up")

            }
        }
        else{

            let pageWidth = scrollView.frame.size.width

            let page = Int(floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)

            let index = IndexPath(row:(scrollView.tag),section:0) //scrollview.tag get the index value of tableview when collection view scroll

        }
相关问题