Ios 可展开视图错误,因为它未注册为观察者
我想要一个可扩展的表视图,但当我尝试扩展表视图单元格时,出现以下错误: 2015-10-04 19:42:25.030侧边栏菜单[7692:304078]***由于未捕获异常“NSRangeException”而终止应用程序,原因:“无法从中删除关键路径“frame”的观察者,因为它未注册为观察者。” 以下是全部源代码: 这是源代码的一部分: 设置ViewController.swiftIos 可展开视图错误,因为它未注册为观察者,ios,swift,uitableview,Ios,Swift,Uitableview,我想要一个可扩展的表视图,但当我尝试扩展表视图单元格时,出现以下错误: 2015-10-04 19:42:25.030侧边栏菜单[7692:304078]***由于未捕获异常“NSRangeException”而终止应用程序,原因:“无法从中删除关键路径“frame”的观察者,因为它未注册为观察者。” 以下是全部源代码: 这是源代码的一部分: 设置ViewController.swift 导入UIKit let cellID=“cell” 类设置ViewController:UITableVie
导入UIKit
let cellID=“cell”
类设置ViewController:UITableViewController{
var selectedIndexPath:NSIndexPath?
重写func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回2
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell=tableView.dequeueReusableCellWithIdentifier(cellID,forIndexPath:indexPath)
cell.textLabel!.text=“测试标题”
返回单元
}
重写func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath){
让previousIndexPath=selectedIndexPath
如果indexPath==selectedIndexPath{
selectedIndexPath=nil
}
否则{
selectedIndexPath=indexPath
}
变量索引路径:数组=[]
如果let previous=previousIndexPath{
INDExpath+=[上一页]
}
如果let current=selectedIndexPath{
INDExpath+=[当前]
}
如果indexPaths.count>0{
tableView.reloadRowsAtIndexPaths(InExpaths,withRowAnimation:UITableViewRowAnimation.Automatic)
}
}
重写func tableView(tableView:UITableView,willDisplayCell:UITableViewCell,ForRowatineIndexPath-indexPath:NSIndexPath){
(单元格为!SettingViewCell).watchFrameChanges()
}
重写func tableView(tableView:UITableView,DiEndDisplayingCell:UITableViewCell,ForRowatineIndexPath indexPath:NSIndexPath){
(单元格为!SettingViewCell).ignoreFrameChanges()
}
重写func tableView(tableView:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat{
如果indexPath==selectedIndexPath{
返回设置ViewCell.expandedHeight
}
否则{
返回设置ViewCell.defaultHeight
}
}
}
SettingViewCell.swift
导入UIKit
类设置ViewCell:UITableViewCell{
@IBVAR弱var标题标签:UILabel!
@IBVAR弱日期选择器:UIDatePicker!
类var expandedHeight:CGFloat{get{return 200}
类var defaultHeight:CGFloat{get{return 44}
func checkHeight(){
datePicker.hidden=(frame.size.height
import UIKit
let cellID = "cell"
class SettingViewController: UITableViewController {
var selectedIndexPath : NSIndexPath?
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
cell.textLabel!.text = "Test Title"
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let previousIndexPath = selectedIndexPath
if indexPath == selectedIndexPath {
selectedIndexPath = nil
}
else {
selectedIndexPath = indexPath
}
var indexPaths : Array<NSIndexPath> = []
if let previous = previousIndexPath {
indexPaths += [previous]
}
if let current = selectedIndexPath {
indexPaths += [current]
}
if indexPaths.count > 0 {
tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic )
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! SettingViewCell).watchFrameChanges()
}
override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! SettingViewCell).ignoreFrameChanges()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath == selectedIndexPath {
return SettingViewCell.expandedHeight
}
else {
return SettingViewCell.defaultHeight
}
}
}
import UIKit
class SettingViewCell : UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!
class var expandedHeight: CGFloat { get {return 200 } }
class var defaultHeight: CGFloat { get {return 44 } }
func checkHeight() {
datePicker.hidden = (frame.size.height < SettingViewCell.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()
}
}
}