gpt4 book ai didi

ios - UITableView 被释放,而键值观察者仍然在其中注册

转载 作者:可可西里 更新时间:2023-11-01 05:01:38 25 4
gpt4 key购买 nike

我正在为大多数具有侧边菜单功能的应用程序使用 MFSideMenu Controller 。所有应用程序在 iOS 7 和之前的版本上运行良好,但在运行 iOS 8 的 iPhone 6 设备上运行不正常(在运行 iOS8 的 iPhone 5 上运行良好)。
我似乎无法弄清楚如何解决我收到的这个错误。单击 LeftMenu UITableView 上的单元格时,应用程序崩溃并出现此错误。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7fc492877e00 of class UITableView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x7fc492634860>

注意:UITableView 是一个 XIB IBOutlet

代码是这样的

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

// This will remove extra space on top of the tableview
self.automaticallyAdjustsScrollViewInsets = NO;
// This will remove extra separators from tableview
self.tblViewMenu.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

// menu titles
arrMenu = [Config returnArrLeftMenu];

// check login status
[self checkLoginStatus];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";

MenuCell *mCell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];

// menu cell
if (mCell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuCell" owner:self options:nil];

for (id oneObject in nib) if ([oneObject isKindOfClass:[MenuCell class]])
mCell = (MenuCell *)oneObject;

}

mCell.selectionStyle = UITableViewCellSelectionStyleBlue;
mCell.contentView.backgroundColor = [UIColor clearColor];
mCell.backgroundColor = [UIColor clearColor];
mCell.lblTitle.text = [arrMenu objectAtIndex:indexPath.row];

return mCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// load selected menu view on to the MAinViewController
MainViewController *mainController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

mainController.title = [arrMenu objectAtIndex:indexPath.row];
mainController.menuIndex = (int)indexPath.row;

UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
NSArray *controllers = [NSArray arrayWithObject:mainController];
navigationController.viewControllers = controllers;
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];

}

最佳答案

您可能会使用如下代码观察 TableView 的属性更改:

[self.tblViewMenu addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]

对观察者(您的类实例)的引用以不安全的未保留语义存储。您必须在 dealloc() 期间清理它以避免应用程序崩溃(ARC 无法为您处理)。

所以你的 dealloc() 应该像这样注销观察者:

- (void)dealloc {
[self.tblViewMenu removeObserver:self forKeyPath:@"frame"];
}

您必须将“frame”替换为您感兴趣的属性的名称。

一定要在适当的地方检查键值观察 (KVO) 的基本概念 Apple Guide

编辑

我刚刚查看了MFSideMenu的源代码.该代码中没有使用 KVO 的迹象,因此崩溃可能不是由该组件引起的。

关于ios - UITableView 被释放,而键值观察者仍然在其中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28018745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com