由于未捕获的异常'NSRangeException'而终止应用程序,原因是:'无法删除观察者 - ios [英] Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer - ios

查看:2286
本文介绍了由于未捕获的异常'NSRangeException'而终止应用程序,原因是:'无法删除观察者 - ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swift开发ios app。我使用的是xcode7.0.1。使用TableViewController。当我点击行时我想扩展并在我再次点击它时折叠。我正在关注 gitHub 的教程。现在我面临错误由于未捕获的异常'NSRangeException'终止应用程序,原因:'无法删除观察者 以获取关键路径frame < X.expandTips 0x145d8d20>因为它没有注册为观察者。'

I am developing ios app by using swift. I am using xcode7.0.1. with TableViewController. I want to expand when i click row and collapse when i click it again. I am following the tutorial from gitHub. Now i am facing the error Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer for the key path "frame" from <X.expandTips 0x145d8d20> because it is not registered as an observer.'

我希望以下代码行会导致问题。

I hope following line of code will causing the issue.

我的UITableViewCell类代码:

My UITableViewCell class code:

func checkHeight(){
        expandaple_view.hidden = (frame.size.height < expandTips.expandedHeight)
    }

    func WatchFrameChanges() {
        addObserver(self , forKeyPath: "frame", options: .New, context: nil )
        checkHeight() 
    }

    func ignoreFrameChanges(){

        removeObserver(self, forKeyPath: "frame")

    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if keyPath == "frame"{
            checkHeight()
        }
    }

在我的TableViewController代码中:

And in my TableViewController code:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
       (cell as! expandTips) . WatchFrameChanges()
    }

    override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        (cell as! expandTips) . ignoreFrameChanges()
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if (indexPath == selectedIndexPath ){
            return expandTips.expandedHeight
        }else {
            return expandTips.defaultHeight
        }
    }

我是ios的新手。我希望这一定是个简单的问题。请有人帮我解决这个问题。

I am new to ios. I hope it must be simple issue. Please someone help me to solve this.

我不知道有什么细节需要在这里发布。如果我想添加更多细节,请发表评论。

I don't know what are the details needs to post here. Please comment if i want to add more details.

推荐答案

从@Leandros的评论我找到了解决方案。只需使用布尔变量跟踪观察者。

From the comment of @Leandros I found the solution. Just tracking the observer using boolean variable.

使用以下代码,我找到了解决方案:

Use of following code, I found the solution:

 var frameAdded = false
 func checkHeight(){

        expanding_view.hidden = (frame.size.height < expandTips.expandedHeight)
    }

    func WatchFrameChanges() {
        if(!frameAdded){
        addObserver(self , forKeyPath: "frame", options: .New, context: nil )
            frameAdded = true
        }
    }

    func ignoreFrameChanges() {
          if(frameAdded){
        removeObserver(self, forKeyPath: "frame")
            frameAdded = false
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if keyPath == "frame"{
            checkHeight()
        }
    }
    deinit {
        print("deinit called");
        ignoreFrameChanges()
    }

这篇关于由于未捕获的异常'NSRangeException'而终止应用程序,原因是:'无法删除观察者 - ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆