简体   繁体   English

如何在选择tableView的行单元格(作为集合视图单元格)时显示视图控制器?

[英]How to show view controller on selecting row cell of tableView(as a collection view cell)?

I have the Main View Controller which has a collection view with its collection view cells each initialized as a tableView to serve multiple rows inside of that collection view cell. 我有主视图控制器,它具有一个集合视图,其集合视图单元格每个都初始化为tableView,以在该集合视图单元格内提供多行。 If you're getting confused, below is the snapshot of the current state. 如果您感到困惑,下面是当前状态的快照。

The problem is when I try to tap a tableView row cell to open another view controller, It fails and a selected state of table view cell is shown. 问题是当我尝试点击tableView行单元格以打开另一个视图控制器时,它失败并显示表视图单元格的选定状态。

Here is the snapshot. 这是快照。

//HomeCollectionViewCell.swift
class HomeCollectionViewCell: UICollectionViewCell {
override func layoutSubviews() {
    super.layoutSubviews()
    setUpCellView()
}
func setUpCellView() {
    let frame = CGRect(x:20, y:20, width: bounds.width - 40, height: 600)
    let cell = CellView(frame: frame)
    contentView.addSubview(cell)
  }
}

//CellView.swift
class CellView: UITableView {
    let quoteCell = "QuoteCell"
    let newsCell = "NewsCell"
    let articleCell = "ArticleCell"
    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: .grouped)
        self.layer.cornerRadius = 15
        self.backgroundColor = .white
        self.dataSource = self
        self.delegate = self
        self.register(QuoteTableViewCell.self, forCellReuseIdentifier: quoteCell)
        self.register(NewsTableViewCell.self, forCellReuseIdentifier: newsCell)
        self.register(ArticleTableViewCell.self, forCellReuseIdentifier: articleCell)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension CellView: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch indexPath.section {
        case 0: return 35
        case 1: return 140
        case 2: return 100
        case 3: return 140
        default: return 0
    }
  }
}
extension CellView: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return categories.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return categories[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.section {
        case 0: let cell = tableView.dequeueReusableCell(withIdentifier: dateCell)
                cell?.textLabel?.text = "Today"
                cell?.textLabel?.font = UIFont.systemFont(ofSize: 30, weight: UIFont.Weight.heavy)

        return cell!
        case 1: let cell = tableView.dequeueReusableCell(withIdentifier: quoteCell) as! QuoteTableViewCell
        return cell
        case 2: let cell = tableView.dequeueReusableCell(withIdentifier: newsCell) as! NewsTableViewCell
        return cell
        case 3: let cell = tableView.dequeueReusableCell(withIdentifier: articleCell) as! ArticleTableViewCell
        return cell
        default: let cell = tableView.dequeueReusableCell(withIdentifier: commonCell)
        cell?.textLabel?.text = "LOL"
        return cell!
    }
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 0: print("Date Selected")
    case 1: print("Quote Selected")
    case 2: print("News Selected")
    case 3: let homeViewController = HomeViewController()
            let articleDetailViewController = ArticleDetailViewController()
//homeViewController.show(articleDetailViewController, sender: homeViewController)//homeViewController.navigationController?.pushViewController(articleDetailViewController, animated: true)
    homeViewController.present(articleDetailViewController, animated: true, completion: nil)
            print("Article selected")
    default: print("LOL")
    }
  }   
}

//HomeViewController.swift //HomeViewController.swift

class HomeViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    setupNavBar()
    view.addSubview(collectionView)
    setUpConstraints()
    configure(collectionView: collectionView)
}
func setUpConstraints() {
    _ = collectionView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 10, leftConstant: 10, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 0)
    collectionView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}

lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.alwaysBounceVertical = true
    cv.clipsToBounds = true
    cv.showsHorizontalScrollIndicator = false
    cv.showsVerticalScrollIndicator = false
    cv.backgroundColor = .clear
    cv.isHidden = false
    return cv
}()
}
private let reuseIdentifier = "Cell"
extension HomeViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
internal func configure(collectionView: UICollectionView) {
    collectionView.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HomeCollectionViewCell
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.bounds.width, height: 600)
}
}

Please tell where I'm doing wrong or What approach should I use? 请告诉我我在哪里做错了,或者应该使用哪种方法?

Note- No use of storyboards/IB. 注意-不使用情节提要/ IB。 Done things programmatically only. 仅以编程方式完成操作。

Give identifiers(" HomeViewController " and " ArticleDetailViewController ") to view controllers and try below code in didSelectRow() . 提供标识符(“ HomeViewController ”和“ ArticleDetailViewController ”)以查看控制器,并尝试在didSelectRow()中使用以下代码。

case 3:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sourceViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "ArticleDetailViewController") as? ArticleDetailViewController
let navigator: UINavigationController = sourceViewController as! UINavigationController                                                                                                                                                       
navigator.pushViewController(destinationViewController!, animated: true)

From what have you done, I want to point out that its not a good idea to present UIViewController from UView . 通过您所做的一切,我想指出,从UView呈现UIViewController并不是一个好主意。 You must write some custom delegates which will get fired once someone taps on those cells in the custom CellView class. 您必须编写一些自定义委托,一旦有人点击自定义CellView类中的这些单元,这些委托将被解雇。 Those delegates must be implemented in the view controller that contains the tableview. 这些委托必须在包含tableview的视图控制器中实现。 From the UIViewController you must write the code to present the new viewcontrollers. 您必须从UIViewController编写代码以呈现新的ViewController。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一个视图控制器的单元格中为索引行加载两个tableview? - how to load two tableview in same view controller with cell for index row? 如何显示通过滑动行在自定义tableview单元内创建的视图? - How to show a view created inside the custom tableview cell by swiping the row? 在表视图控制器中选择单元格时在容器视图中显示数据 - Show data in container view on selecting a cell in Table View Controller 点击单元格时如何显示tableview单元格编辑视图? - How to show tableview cell edit view when tap the cell? 在表格视图单元格中选择文本并返回上一个视图 - Selecting the text in the tableview cell and returning to previous view 如何在集合视图单元格的某一部分中添加多个数组,该集合视图位于tableView单元格内? - How to add multi arrays in one section of Collection View Cell which that collection view inside tableView cell? 如何在表格视图单元格上按以向视图控制器显示导航控制器中的文本 - How to press on a tableview cell to present a view controller with the text in navigation controller Swift 2.1-如何将集合视图单元格的索引行传递给另一个视图控制器 - Swift 2.1 - How to pass index row of collection view cell to another view controller 集合视图的单元格未显示在行中 - cell of collection view not displaying in row 单击不同的TableView单元格之前,TableView单元格不会加载View Controller - TableView Cell not loading View Controller until Different TableView Cell is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM