Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios TableViewController不接收触摸_Ios_Swift_Uitableview - Fatal编程技术网

Ios TableViewController不接收触摸

Ios TableViewController不接收触摸,ios,swift,uitableview,Ios,Swift,Uitableview,注意:我最初问这个问题,想知道为什么没有人打电话给DidSelectRowatinex。深入研究之后,我意识到我的核心问题是我的一个观点没有收到触动事件。我将把这个问题留给后人,问一个更清楚的版本如果管理员想关闭或删除此问题,请继续操作。 我在我的应用程序中实现了一个边栏(比如汉堡包菜单),我正在使用UITableViewController来实现它。我可以得到侧边栏显示和细胞初始化正确 我的代码非常简单,我在这里包括了所有的代码。现在我只有一个主屏幕的视图控制器和一个侧栏的表视图控制器。感谢您

注意:我最初问这个问题,想知道为什么没有人打电话给DidSelectRowatinex。深入研究之后,我意识到我的核心问题是我的一个观点没有收到触动事件。我将把这个问题留给后人,问一个更清楚的版本如果管理员想关闭或删除此问题,请继续操作。

我在我的应用程序中实现了一个边栏(比如汉堡包菜单),我正在使用UITableViewController来实现它。我可以得到侧边栏显示和细胞初始化正确

我的代码非常简单,我在这里包括了所有的代码。现在我只有一个主屏幕的视图控制器和一个侧栏的表视图控制器。感谢您的帮助

更新:问题似乎不是没有调用DidSelectRowatineXpath——而是我的SimpleTableViewController根本没有收到任何触摸我尝试在ViewController和SimpleTableViewController中覆盖“touchesBegind:withEvent:”:ViewController接收到的触摸很好,但table view控制器似乎没有接收到任何触摸

进一步更新:我意识到问题与我如何制作动画以显示屏幕右侧的tableview有关。现在,我正在将主应用程序视图“推”到左侧,这将“拉”containerView。当我这样做时,当我点击containerView时,它不会被触动

然而!如果我将containerView“拉”到主视图的顶部,就会收到良好的效果关于iOS认为触摸合法的屏幕“活动”部分,我可能遗漏了一些基本内容?

代码如下:

之前--已损坏

@IBAction func push() {
            // containerView is "pulled" alongside self.view
            UIView.animateWithDuration(3.0) {
                self.view.frame = CGRectMake(self.originalX - 250, self.originalY, self.view.frame.width, self.view.frame.height)
                }
        }
下面是一个gif,显示了当触摸不起作用时应用程序的外观

After--WORKS

@IBAction func push() {
            // containerView is "pulled" on top of self.view
            UIView.animateWithDuration(3.0) {
                self.containerView.frame = CGRectMake(self.originalX - 250, self.originalY, 250, self.frameHeight)
                }
        }
下面是一个gif,显示了当触控功能正常时,应用程序的外观。我添加了一些代码来更改主视图的背景色,以说明正在接收触摸

代码如下-我以前在这里有我的整个实现,但我已经对它进行了编辑,只包括相关部分

首先,主视图控制器:

import UIKit

class ViewController: UIViewController, SimpleTableViewControllerDelegate {

    var x = UIView()
    var y = UIView()

    var containerView = UIView()

    let animationDuration = 1.5;
    let delayTime = 0.25;

    let animationTime = 0.25;

    var tableViewController = SimpleTableViewController()

    override func viewDidLoad() {
        super.viewDidLoad()         

        originalX   = self.view.frame.origin.x
        originalY   = self.view.frame.origin.y
        frameHeight = self.view.frame.height
        frameWidth  = self.view.frame.width

        containerView.frame = CGRectMake(frameWidth, originalY, 250, frameHeight)
        containerView.backgroundColor = UIColor.magentaColor()
        containerView.clipsToBounds = false

        view.addSubview(containerView)

        tableViewController.items = ["Lemon", "Lime", "Agave"]
        tableViewController.delegate = self
        tableViewController.tableView.dataSource = tableViewController
        tableViewController.tableView.delegate = tableViewController
        tableViewController.tableView.frame = containerView.bounds
        tableViewController.tableView.backgroundColor = UIColor.lightGrayColor()
        tableViewController.tableView.scrollsToTop = false
        tableViewController.tableView.separatorStyle = .None
        tableViewController.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0, bottom: 0, right: 0)
        tableViewController.tableView.reloadData()

        addChildViewController(tableViewController)
        containerView.addSubview(tableViewController.tableView)
        tableViewController.didMoveToParentViewController(self)
    }

    func didSelectRowAtIndexPath(indexPath: NSIndexPath) {
        NSLog("[ViewController] invoked didSelectRowAtIndexPath with \(indexPath.row)")
    }        

    var originalX : CGFloat!
    var originalY : CGFloat!
    var frameHeight : CGFloat!
    var frameWidth : CGFloat!

    @IBAction func push() {
        UIView.animateWithDuration(animationTime, delay: 0, options: .CurveLinear, animations: {
            self.view.frame = CGRectMake(self.originalX - 250, self.originalY, self.view.frame.width, self.view.frame.height)
            }, completion: { (var b) -> Void in

        })
    }

    @IBAction func pull() {
        UIView.animateWithDuration(animationTime, delay: 0, options: .CurveLinear, animations: {
            self.view.frame = CGRectMake(self.originalX, self.originalY, self.view.frame.width, self.view.frame.height)
            }, completion: { (var b) -> Void in

        })
    }        

}
    //Add these two lines
    tableViewController.tableView.dataSource = tableViewController
    tableViewController.tableView.delegate = tableViewController

    tableViewController.delegate = self
    tableViewController.tableView.frame = containerView.bounds
    tableViewController.tableView.backgroundColor = UIColor.lightGrayColor()
    tableViewController.tableView.scrollsToTop = false
    tableViewController.tableView.separatorStyle = .None
    tableViewController.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0, bottom: 0, right: 0)
    tableViewController.tableView.reloadData()
值得一提的是,如果您想查看整个应用程序(它只是一个学习应用程序),您可以点击这里:

在ViewController的viewDidLoad()中:

import UIKit

class ViewController: UIViewController, SimpleTableViewControllerDelegate {

    var x = UIView()
    var y = UIView()

    var containerView = UIView()

    let animationDuration = 1.5;
    let delayTime = 0.25;

    let animationTime = 0.25;

    var tableViewController = SimpleTableViewController()

    override func viewDidLoad() {
        super.viewDidLoad()         

        originalX   = self.view.frame.origin.x
        originalY   = self.view.frame.origin.y
        frameHeight = self.view.frame.height
        frameWidth  = self.view.frame.width

        containerView.frame = CGRectMake(frameWidth, originalY, 250, frameHeight)
        containerView.backgroundColor = UIColor.magentaColor()
        containerView.clipsToBounds = false

        view.addSubview(containerView)

        tableViewController.items = ["Lemon", "Lime", "Agave"]
        tableViewController.delegate = self
        tableViewController.tableView.dataSource = tableViewController
        tableViewController.tableView.delegate = tableViewController
        tableViewController.tableView.frame = containerView.bounds
        tableViewController.tableView.backgroundColor = UIColor.lightGrayColor()
        tableViewController.tableView.scrollsToTop = false
        tableViewController.tableView.separatorStyle = .None
        tableViewController.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0, bottom: 0, right: 0)
        tableViewController.tableView.reloadData()

        addChildViewController(tableViewController)
        containerView.addSubview(tableViewController.tableView)
        tableViewController.didMoveToParentViewController(self)
    }

    func didSelectRowAtIndexPath(indexPath: NSIndexPath) {
        NSLog("[ViewController] invoked didSelectRowAtIndexPath with \(indexPath.row)")
    }        

    var originalX : CGFloat!
    var originalY : CGFloat!
    var frameHeight : CGFloat!
    var frameWidth : CGFloat!

    @IBAction func push() {
        UIView.animateWithDuration(animationTime, delay: 0, options: .CurveLinear, animations: {
            self.view.frame = CGRectMake(self.originalX - 250, self.originalY, self.view.frame.width, self.view.frame.height)
            }, completion: { (var b) -> Void in

        })
    }

    @IBAction func pull() {
        UIView.animateWithDuration(animationTime, delay: 0, options: .CurveLinear, animations: {
            self.view.frame = CGRectMake(self.originalX, self.originalY, self.view.frame.width, self.view.frame.height)
            }, completion: { (var b) -> Void in

        })
    }        

}
    //Add these two lines
    tableViewController.tableView.dataSource = tableViewController
    tableViewController.tableView.delegate = tableViewController

    tableViewController.delegate = self
    tableViewController.tableView.frame = containerView.bounds
    tableViewController.tableView.backgroundColor = UIColor.lightGrayColor()
    tableViewController.tableView.scrollsToTop = false
    tableViewController.tableView.separatorStyle = .None
    tableViewController.tableView.contentInset = UIEdgeInsets(top: 64.0, left: 0, bottom: 0, right: 0)
    tableViewController.tableView.reloadData()

将另一个视图控制器的视图添加到视图层次结构时,必须让父视图和子视图都知道,以便父视图可以将相关消息转发给子视图。这在苹果的“实现自定义容器视图控制器”一节中进行了解释

在您的情况下,您需要以下内容:

[self addChildViewController:tableViewController];
containerView.addSubview(tableViewController.tableView)
[tableViewController didMoveToParentViewController:self];

为什么要调用该委托?.didselectRowatineXpath(indexPath)再次调用@AshishKakkad啊——好电话——我还没意识到。让我试一试。并检查您的表视图代理。@AshishKakkad您在上面看到的是我所拥有的-没有其他东西。感谢您的回答Joseph-我已经用您的建议更新了上面的代码。当我运行它时,不幸的是,仍然没有发生任何事情。嘿@pbasdf,谢谢你的回答。我试过了,并更新了我的问题以反映你的建议。不幸的是,没有任何更改-当我单击“表视图”单元格时,没有调用DidSelectRowatineXpath。我想我可能已经找到了一个线索——看起来我的“表视图”控制器甚至没有收到触摸!我将更新我的问题。您的容器视图超出了主视图的边界,因此将不会收到接触。您可以覆盖此行为(请参见Apple的),但在您的情况下,仅将主视图的框架扩展250,使容器视图在其范围内,可能更容易。扩展视图宽度有效!我想知道这样做是否有任何潜在的缺点?我不认为有任何缺点,但这不是我的专业领域。