对UITableViewCell内的UITableView行执行操作以打开另一个视图控制器

时间:2018-02-22 12:17:02

标签: ios swift uitableview

如何将didSelectRowAt动作添加到表格内的tableview中查看单元格[Swift]?当我试图打开另一个视图控制器时,我甚至无法声明故事板..

enter image description here  我在表视图单元格中有一个表视图。在表视图单元格中选择表视图行的操作需要与父表视图单元格的didSelectRowAt()相同。实际父表视图单元格的didSelectRowAt()导航到另一个视图控制器。所以我试图调用视图控制器,但无法这样做......有没有其他办法绕过这个问题......

5 个答案:

答案 0 :(得分:0)

你可以试试这个:

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

        ///do stuff here
}

希望它有所帮助!!

答案 1 :(得分:0)

看起来你已经将你的单元类本身声明为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)
        }
    } 

    ..
}

答案 2 :(得分:0)

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

formControlName

但是你的问题不同我想更新你的问题。

答案 3 :(得分:0)

TableViewCell无法呈现ViewController,

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

将它放在tableview单元格中(在另一个表格视图单元格内)didSelectRowAt方法

2018-02-22 13:52:21 DEBUG: urllib3.connectionpool: Starting new HTTPS connection (1): stackoverflow.com
2018-02-22 13:52:21 DEBUG: urllib3.connectionpool: https://stackoverflow.com:443 "GET / HTTP/1.1" 200 36997

在viewController viewDidLoad方法中添加观察者

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

并在此处理您的通知

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

答案 4 :(得分:0)

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

  1. 在tableview单元格中声明弱委托。 一个。发送有效载荷。 i,e(所选单元格的索引路径)到viewController。
  2. 在viewController中执行操作。
  3. 通知也可用于相同但通常不鼓励通知,我们应尽量在极少数情况下使用它们。

    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符合单元格委托并从那里执行操作。

    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)
          }
        }