【问题标题】:Action to UITableView row inside UITableViewCell to open another view controller对 UITableViewCell 内的 UITableView 行的操作以打开另一个视图控制器
【发布时间】:2018-02-23 21:01:51
【问题描述】:

如何将 didSelectRowAt 动作添加到表格视图单元格 [Swift] 内的表格视图中?当我试图打开另一个视图控制器时,我什至无法声明情节提要..

我在表格视图单元格中有一个表格视图。在表格视图单元格内选择表格视图行的操作需要与父表格视图单元格的 didSelectRowAt() 的操作相同。实际的 didSelectRowAt() 的父表视图单元格导航到另一个视图控制器。所以我正在尝试调用视图控制器,但无法这样做...有没有其他方法可以解决这个问题...

【问题讨论】:

  • 不要将屏幕截图复制和粘贴代码置于问题中
  • 您在哪个类中添加了委托方法?如果要访问 self.storyboard 属性,请确保它是 UIViewController 的子类
  • 要使用storyboard,你得先声明一下吧?
  • 让 storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

标签: ios swift uitableview


【解决方案1】:

TableViewCell 无法呈现 ViewController,

如果您的 tableViewCell 包含另一个 tableView,并且您单击其中一个单元格,则必须通知 ViewController 该单元格已被按下

把它放在你的表格视图单元格中(在另一个表格视图单元格中)didSelectRowAt 方法

NotificationCenter.default.post(name: Notification.Name("aNotificationName"), object: nil)

在你的 viewController viewDidLoad 方法中添加观察者

NotificationCenter.default.addObserver(self, selector: #selector(handleNotificationFuncName, name: NSNotification.Name(rawValue: "aNotificationName"), object: nil)

并在此处处理您的通知

func handleNotificationFuncName(_ notification: Notification) {
    let storyboard = UIStoryboard(name: "StoryboardName", bundle: Bundle.main)

    if let vc = storyboard.instantiateViewController(withIdentifier: "pdfDisplayViewController") as? pdfDisplayViewController {
        present(vc, animated: true, completion: nil)
    }
}

【讨论】:

  • 谢谢..它帮助很大...当我再次遇到同样的问题时,在故事板中禁用表格视图上的用户交互对我有帮助..
【解决方案2】:

你可以试试这个:

let storyboard = UIStoryboard(name: "StoryboardName", bundle: Bundle.main)
if let vc = let vc = storyboard.instantiateViewController(withIdentifier: "pdfDisplayViewController") as? pdfDisplayViewController {

        ///do stuff here
}

希望对你有帮助!

【讨论】:

    【解决方案3】:

    看起来您已将单元类本身声明为 tableView 的委托,而是使您当前的 ViewController 符合 UITableViewDelegate,然后实例化视图控制器并将其推送到堆栈:

    class MyViewController: UITableViewDelegate {
    
        ..
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pdfdisplayviewcontroller") as? pdfdisplayViewController {
                navigationController.pushViewController(controller, animated: true)
            }
        } 
    
        ..
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用以下代码从一个 ViewController 导航到另一个 ViewController。

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
                  let storyboard = UIStoryboard(name: "YourStoryBoardName", bundle: nil)
                  let controller = storyboard.instantiateViewController(withIdentifier: "pdfDisplayViewController") as! pdfDisplayViewController
                  self.navigationController?.pushViewController(controller, animated: false)
                  self.navigationController?.navigationBar.isHidden = true
              }
      

      但我认为你的问题不同,所以更新你的问题。

      【讨论】:

        【解决方案5】:

        导航到另一个 ViewController 或从 tableView Cells 执行任何交互不是最佳实践。我更喜欢您使用委托模式来获得所需的功能。

        1. 在表格视图单元格中声明弱委托。 一个。发送有效载荷。 i,e(所选单元格的索引路径)到 viewController。
        2. 在 viewController 中执行操作。

        通知也可以用于相同的目的,但通常不鼓励使用它们,我们应该尝试在极少数情况下使用它们。

        protocol InnerTableCellDelegate: class {
           func didSelectedRowWith(indexPath: IndexPath)
        }
        class InnerTableCell: UITableViewCell {
        
        weak var delegate: InnerTableCellDelegate? = nil 
         .
         .
         . 
             // In tableCellDelegate
             func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
               if let safeDelegate = self.delegate {
                   safeDelegate.didSelectedRowWith(indexPath: indexPath)
                }     
             }
          }  
        

        现在您必须在父 viewController 中拥有相同的 tableView 委托和数据源。在 cellForRowAt(...) 方法中,将单元格委托设置为 self。

        class ViewController: UIViewController {
          // MARK: - TableView Datasouce & Delegates
          func cellForRowAt(tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
             let cell: InnerTableCell = tableView.dequeReusableCellWithIdentifier(reusableIdentifier: "InnerTableViewCell", indexPath: IndexPath) as! InnerTableViewCell
             cell.delegate = self
             return cell
          }
        } 
        

        现在,让您的 viewcontroller 符合 cell 委托并从那里执行操作。

        extension ViewController: InnerTableCellDelegate {
            func didSelectedRowWith(indexPath: IndexPath) {
               let storyboard = UIStoryboard(name: "storyboard", bundle: nil)
               let viewC = storyboard.instantiateViewController(withIdentifier: "DestinationViewC") as! DestinationViewC
               self.navigationController?.pushViewController(controller, animated: false)
              }
            } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多