【问题标题】:ValueType "tableViewCell" has no member navigationControllerValueType "tableViewCell" 没有成员 navigationController
【发布时间】:2017-11-06 19:07:53
【问题描述】:

我在 tableView 单元格中有一个 collectionView。我想点击 collectionView 单元格并使用传入的信息转到 detailTableView。我没有太多工作。已经做了一些研究,它指出 tableView 单元格既没有故事板也没有导航控制器。我如何能够将数据传递到detailTableView。这是我的实现,可能不正确

class PopularTableViewCell: UITableViewCell {
     var books = [CKRecord]()
     var detailViewController: DetailViewController!
} 


extension PopularTableViewCell: UICollectionViewDataSource {
   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let record = books[indexPath.row]
        let storyBoard = UIStoryboard(name: "Home", bundle: nil)
        if let detailVC = storyBoard.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
            detailVC.bookRecord = record
           detailViewController.navigationController?.pushViewController(detailVC, animated: true)
        }
    }
}

【问题讨论】:

    标签: ios swift uitableview uicollectionviewcell pushviewcontroller


    【解决方案1】:

    您应该从包含 tableview 的根视图控制器推送视图控制器:

    self.navigationController.pushViewController(detailVC, animated: true)
    

    可以使用自定义委托传递来自集合视图的数据,例如:

    protocol CustomDelegate: class {
        func didSelectItem(record: CKRecord)
    }
    
    weak var delegate: CustomDelegate?
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let record = books[indexPath.row]
        delegate?.didSelectItem(record: record)
    }
    

    然后在 tableview 单元格中另一个委托:

    extension TableViewCell: CustomDelegate {
        func didSelectItem(record: CKRecord) {
            delegate?.didSelectItem(record: record)
        }
    }
    

    最后在视图控制器中:

    extension ViewController: CustomDelegate {
        func didSelectItem(record: CKRecord) {
            let storyBoard = UIStoryboard(name: "Home", bundle: nil)
            if let detailVC = storyBoard.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
                detailVC.bookRecord = record
                self.navigationController?.pushViewController(detailVC, animated: true)
            }
        }
    }
    

    PS:不要忘记像这样连接代表:

    cell.delegate = self
    

    【讨论】:

    • 我要试试这个,但 didSelectItemAt indexPath 方法在我的 tableViewCell 中。一切都在tableViewCell 中实现。我只有一个 tableViewController 我只是在其中更改 displayHeaderView
    • 知道了,所以您只需要从 tableViewCell 通知 tableViewController 的单个自定义委托调用
    • @Alexander Kim 我仍然无法从单元格中找到对属性的引用,我已经在 didselectItem 方法中实现了委托方法,但这是在单元格内部。我在哪里可以打电话给func,就像你在扩展TableviewCell: CustomDelegate中所做的那样
    • 自定义委托方法应该在tableViewController中实现
    • 在 tableViewController cellForRow 中写入 cell.delegate = self 然后在 tableViewController 中实现 CustomDelegate didSelectItem 函数你可以把它写成扩展或者像这样:ViewController: UITableViewController, CustomDelegate
    【解决方案2】:

    你可以在你的集合视图单元格上创建一个委托,然后当它被点击时通知表格视图,然后你可以做任何你想做的事情。

    只有集合视图并添加第二个部分来管理详细视图会更简单吗?

    【讨论】:

      【解决方案3】:

      我在集合视图中遇到了同样的问题。经过一番尝试,我意识到解决这个问题的概念是找出那些视图的控制器,无论它们是表格视图还是集合视图,然后使用这个控制器导航到新的控制器。

      • 声明一个稍后可以在表格视图中访问的控制器变量。 var controller = ViewController()(按照我的风格,我更喜欢写成全局变量)

      • 将声明的控制器变量赋值给self,以便让引用指向这个变量。

         class ViewController: UIViewController{
          
          
           override func loadView() {
              controller = self
           }  
         }
      
      • 在您的案例表视图的其他视图中使用此控制器的导航推送方法。
          func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
               controller.navigationController?.pushViewController(DetailViewController(), animated: true)
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多