Swift - 如何在UITableViewCell中使用CollectionViewCell打开另一个viewcontroller

时间:2016-04-08 05:17:15

标签: ios swift uitableview delegates

我在iOS / Swift中真的很新,我在一个小项目中。在这个项目中,我在ViewController中有一个UITableView。我在UITableViewCell旁边有另一个文件自定义CollectionViewCell。 我想当用户点击collectionview中的一个单元格时,它将打开另一个ViewController并从该collectionviewcell获取数据。 这是我的uitableview swift文件:

class IndexRow: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var names:[String] = ["Movie 1","Movie 2","Movie 3","Movie 4","Movie 5","Movie 6"]
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.registerClass(indexOneMovie.self, forCellWithReuseIdentifier: "onemovie")
    let nib = UINib(nibName: "indexOneMovie",bundle: nil)
    collectionView.registerNib(nib, forCellWithReuseIdentifier: "onemovie")
    self.collectionView.backgroundColor = UIColor.clearColor()
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    print("Hello")
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return names.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("onemovie", forIndexPath: indexPath) as! indexOneMovie
    cell.poster.image = UIImage(named: "poster.jpg")
    cell.name.text = names[indexPath.row]
    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print(indexPath.item)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let itemsPerRow:CGFloat = 2
    let hardCodedPadding:CGFloat = 0
    let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
    let itemHeight = collectionView.bounds.height - (hardCodedPadding)
    return CGSize(width: itemWidth, height: itemHeight)
}

我怎么做?

2 个答案:

答案 0 :(得分:2)

你可以这样做:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print(indexPath.item)
    let name = names[indexPath.item]
    let distinationViewController = DistinationViewController()
    distinationViewController.name = name
    if let navVC: UINavigationController = UIApplication.sharedApplication().keyWindow?.rootViewController as? UINavigationController {
    navVC.pushViewController(distinationViewController, animated: true)
    }
}

这只是一种方法,我不知道你要推送哪个视图或你的名字数组包含什么,所以请相应地更改这些内容。

从uiapplication获取root navigationcontroller并对其执行推送。

答案 1 :(得分:2)

好的,我最近在我的应用程序中实现了相同的内容,这些是我最初提到的链接 -

https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

http://www.thorntech.com/2015/08/want-your-swift-app-to-scroll-in-two-directions-like-netflix-heres-how/

您正在使uicollectionview委托确认到uitableview单元格,因此您无法呈现或推送到其他视图控制器。

这是我的代码希望它能帮到你

包含uitableview的homeController.swift

    extension HomeController : UITableViewDelegate {

        func tableView(tableView: UITableView,willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {

          guard let tableViewCell = cell as? TableViewCell else { return }
            //here setting the uitableview cell contains collectionview delgate conform to viewcontroller
          tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row, andForSection: indexPath.section)
          tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
        }

        func tableView(tableView: UITableView,didEndDisplayingCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {

            guard let tableViewCell = cell as? TableViewCell else { return }
            storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
        }

    }



    extension HomeController: UICollectionViewDelegate, UICollectionViewDataSource {
        func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
          let element : [CollectionViewElement] =  self.returnCollectionViewElementAccordingToIndex(collectionView.tag)
            return element.count
        }

      func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as! horizontalCollectionViewCell

          let element : [CollectionViewElement] =  self.returnCollectionViewElementAccordingToIndex(collectionView.tag)

          cell.cellTitleLabel.text = element[indexPath.row].videos.title
          cell.cellGenerLabel.text = element[indexPath.row].videos.gener

          return cell
      }

      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){


        print("collectionviewtag:\(collectionView.tag) +  indexpathrow:\(indexPath.row)")
        //from here you can do push or present to anyview controller
        // collectionviewtag is tableView cell row value and indexpathrow return collectionView cell row value.



      }


    }

TableViewCell.swift:包含collectionView

的自定义UITableViewCell
    class TableViewCell: UITableViewCell {


        @IBOutlet  private weak  var collectionView: UICollectionView!

        @IBOutlet weak var cellLabel: UILabel!

        @IBOutlet weak var cellButton: UIButton!

        @IBAction func CellButtonActionTry(sender: UIButton) {

          print("Dude  \(cellButton.tag)")

        }




        var collectionViewOffset: CGFloat {
          get {
            return collectionView.contentOffset.x
          }

          set {
            collectionView.contentOffset.x = newValue
          }
        }

        func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>
          (dataSourceDelegate: D, forRow row: Int , andForSection section : Int) {

            collectionView.delegate = dataSourceDelegate
            collectionView.dataSource = dataSourceDelegate
            collectionView.tag = row // tableView indexpathrow equals cell tag
            collectionView.reloadData()
        }




        override func awakeFromNib() {
              super.awakeFromNib()
              // Initialization code
          }

          override func setSelected(selected: Bool, animated: Bool) {
              super.setSelected(selected, animated: animated)

          }

    }