iphone - 尝试由不同的类加载时 TableView 崩溃

标签 iphone ios uitableview memory uiviewcontroller

我有这个应用程序(联系人样式),您可以在其中向 TableView 添加实例,并且它可以很好地向列表添加对象。该列表完美地将它们呈现给详细 View 。我可以在我的 MasterViewController(导航 Controller 的 UITableViewController、rootControler)中多次调用“添加项目”(一个带有 TableView 的 UIViewController) View 。

当我看到这行得通,并想扩展我的应用程序的功能以让用户编辑他们在列表中的当前对象时,我在 detailView 的导航 Controller 中添加了一个“编辑”按钮,这个 UiBarButton 触发了一个尝试加载用于在 TableView 中添加项目的相同“添加项目” View 的方法。这就是奇怪的事情开始发生的地方。

如果我运行应用程序并点击 ListView 中的现有对象,我将成功进入详细 View 。如果我随后点击“编辑”按钮,“添加项目” View 会很好地加载当前正在编辑的对象的数据。然后我可以多次取消/保存/关闭“添加项目” View ,只要我继续从详细 View 调用它即可。如果我随后返回 ListView 并尝试添加项目,应用程序就会崩溃。

如果我启动该应用程序,并且在其他任何事情之前,我从主 ListView 加载“添加项目”,该 View 将完美加载,并且我可以根据需要多次完美地添加对象。如果我随后转到详细 View 并尝试编辑该对象,应用程序就会崩溃。

简单地说(如果你懒得看上面的内容):每当我尝试从另一个 Controller 加载“添加项目” View 时,应用程序崩溃第一次。

这是崩溃日志:

    *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], 
    /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
     2012-03-26 21:22:49.087 Vex Try 5[2749:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
    'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
    *** First throw call stack: ...

显然,它第二次尝试加载时,cellForRowAtIndexPath 返回了一个奇怪的单元格。

下面是我在 ListView 中的调用方式:

- (void)insertNewObject:(id)sender
    {
        if (!_objects) {
             _objects = [[NSMutableArray alloc] init];
        }

        BIDVexTeam *newTeam = [[BIDVexTeam alloc] init];
        [_objects insertObject:newTeam atIndex:0];

        if (!self.addNew) {
            self.addNew = [[BIDEditViewController alloc] init];
        }

        if (!self.editNavController) {
            self.editNavController = [[UINavigationController alloc] initWithRootViewController:self.addNew];
        }

        addNew.title = @"Add New";
        addNew.team = newTeam;
        addNew.parent = self;

        [self presentModalViewController:self.editNavController animated:YES];

    }

下面是我在详细 View 中调用它的方式:

    - (IBAction)editTeam:(id)sender {

        if (!self.editView) {
            self.editView = [[BIDEditViewController alloc] initWithNibName:@"BIDEditViewController" bundle:[NSBundle mainBundle]];
        }

        if (!self.editNavController) {
            self.editNavController = [[UINavigationController alloc] initWithRootViewController:self.editView];
        }

        editView.title = [NSString stringWithFormat:@"Edit %@", detailTeam.number];
        self.editView.team = self.detailTeam;

        [self presentModalViewController:editNavController animated:YES];
    }

编辑:这是我的 cellForRowAtIndexPath 方法...没有将其粘贴在这里因为它太长了,还有我正在尝试实现的屏幕截图:

http://www.clubpenguinaccess.com/extra/cellforrow-method.rtf

screenshot http://www.clubpenguinaccess.com/extra/app-screenshot.png

最佳答案

这肯定表明有可能崩溃。执行此操作的任何行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

...如果没有已分配的可重用单元格,可能会返回 nil。在某些情况下,您会进行分配,例如在这种情况下:

} else if (indexPath.row == kImageRow) {

    static NSString *ImageCellIdentifier = @"ImageCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

.. 但在其他几个地方没有。崩溃的一个很好的假设是它正在运行一条没有触发任何单元格分配的路径。

请搜索所有出队调用,添加一个 nil 返回检查,并分配一个合适的单元格。我认为您的崩溃会得到修复。

关于iphone - 尝试由不同的类加载时 TableView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9883479/

相关文章:

java - 如何创建简单的网络服务

ios - 生产环境中的 NSLog 语句会发生什么?

ios - 具有多个单元附件按钮的表格 View 应一键打开和关闭弹出窗口

ios - swift 3 : Making a Pause Menu in SpriteKit by overlaying a SKView?

ios - UITableView UITableViewCellStyle.Value1 TextLabel与DetailTextLabel重叠

iphone - 当我在谷歌地图中时,从 UIWebView 退出键盘

ios - 如何在 iOS 7 中获取设备或 iPad 的唯一编号?

iphone - sqlite 准备语句错误-没有这样的表

ios - 自定义 TableViewCell | swift

ios - Swift:如何从 TableView 中删除重复项?