UITableView滚动崩溃App [英] UITableView scrolling crashes App

查看:134
本文介绍了UITableView滚动崩溃App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言:我是iPhone SDK,Obj-C,Interface Builder和Cocoa的新手。我可能做错了。

Preface: I am new to the iPhone SDK, Obj-C, Interface Builder and Cocoa. I'm likely doing something obviously wrong.

问题
我有一个 UITableView 如果滚动它会崩溃。它将滚动一点以显示底部最半隐藏单元格的完整单元格,但不会加载下一个单元格。同样地,如果我滚过顶部以完全隐藏最底部的单元格,并且橡皮筋向后显示该单元格,它将在显示之前崩溃。这让我感到奇怪,因为它正在正确地绘制了11个单元中的前7个单元。单元格数据位于 NSArray 中,在 UITableViewController 中链接为 dataSource UITableView code>和委托。它在视图初始化时有效。

Question: I have a UITableView which crashes if I scroll it. It will scroll a little to reveal the full cell of the bottom most half-hidden cell, but won't load the next one. Similarly, if I scroll past the top to fully hide the bottom most cell, and it rubber bands back to show that cell, it will crash before showing it. This strikes me as odd because it is drawing the first 7 of 11 cells correctly. The cell data is in an NSArray, in a UITableViewController linked as both the dataSource and delegate for the UITableView in Interface Builder. It works when the view initializes.

我正在制作一个应用程序我以为我将在2天前完成它只是计算组合并显示它们的列表我认为这将是一个方便的滚动表视图。现在,它甚至不计算所有内容, DataSource 中的 NSArray 初始化一次,其中包含一些字符串,如 @Hello @World

I'm making an App I thought I'd be done with 2 days ago that just calculates combinations and displays a list of them in what I thought would be a convenient scrolling table view. Right now, it doesn't even calculate everything, the NSArray in the DataSource is initialized once with some strings like @"Hello" and @"World".

重现的步骤
因为我正在使用IB,所以我无法准确地向您展示代码中的完整故事。所以我要描述我到目前为止所做的事情并希望它不会让你困倦。

Steps to reproduce: Because I'm using IB, I can't exactly show you the full story in code. So I'm going to describe what I did so far and hope it doesn't make you sleepy.

在Xcode中制作了一个新的Tab Bar Application,因为我想要2个标签,我不想要导航栏或全屏幕表。我将 MainWindow.xib 的第一个标签视图移动到 FirstView.xib 作为给定<​​$的模拟C $ C> SecondView.xib 。这很好用。我修改了视图以包含两个 UITextField 用于输入,以及一个 UITableView 用于输出。这很有效,但桌子是空的。我将 UITableViewController 子类化,其中我填充了 NSArray 属性名为的组合有11个字符串,然后添加

Made a new "Tab Bar Application" in Xcode, because I want 2 tabs, and I don't want a nav bar nor a full screen table. I moved the MainWindow.xib's first tab view out into FirstView.xib as an analogue to the given SecondView.xib. This worked nicely. I modified the view to contain two UITextFields for inputs, and a UITableView for output. This worked but the table was empty. I subclassed the UITableViewController wherein I populated an NSArray property named combinations with 11 strings, and then added

// Set up the cell...
cell.userInteractionEnabled = NO;
cell.text = [combinations objectAtIndex:indexPath.row];

只有评论。在IB中,我将表视图控制器添加到 FirstView.xib 并设置它的类名以匹配此名称新的子类,并在我的视图中将表视图控制拖动到此组合表视图控制器两次。链接 dataSource 委托后。虽然如果仅链接 dataSource ,我会得到相同的行为。

where there had only been the comment. In IB I added a Table View Controller to the FirstView.xib and set it's class name to match the name of this new subclass, and control-dragged the Table View in my view onto this Combinations Table View Controller twice. Once linking the dataSource and once the delegate. Although I get the same behavior if only the dataSource is linked.

这会运行并填充表格的可见行( 6.5)使用 dataSource 组合中的前7个值。我可以向下滚动0.5个单元格,然后再向上滚动。但如果我向上或向下滚动超过0.5个单元格,应用程序将崩溃。报告中的解释如下:

This runs and populates the table's visible rows (6.5) with the first 7 values in the dataSource combinations. I can scroll 0.5 cells down, and then back up. But if I scroll more than 0.5 cells up or down the app will crash. The explanation in the report reads:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[NSCFTimer tableView:cellForRowAtIndexPath:]:
unrecognized selector sent to instance 0x52ca40'

我没有制作 NSCFTimer 也没有将它链接到我的表格视图,我怀疑是 cellForRowAtIndexPath 正是应该转到我的 dataSource 委托所以我很困惑为什么它误入歧途以及它最终走向何方。

I didn't make an NSCFTimer nor did I link it to my Table View, I suspect that the cellForRowAtIndexPath is exactly a message that should have gone to my dataSource or delegate so I'm confused at why it went astray and where it ended up going.

更新:感谢您的回答和评论。我的问题似乎是 CombinationsTableViewController UITableViewController 的子类)实际上没有在我的代码中的任何特定位置实例化。它确实被创建为一段时间(当加载 FirstView.xib 时)并且显然是在初始tableView填充7个单元格时进行管理,然后被释放。因此,我需要确定在何处/如何对此控制器进行保留引用。我的应用程序代理应该有一些持有此控制器的插座,可以链接为 xib 中的实例。是的,我是新手。我知道我可以通过避免IB并在代码中明确地做事来消除这些麻烦,但我想我想学会灵活地使用IB。

Update: Thanks for the answers and comments. My problem seems to be that the CombinationsTableViewController (subclass of UITableViewController) is actually not instantiated any particular place in my code. It does get created as some time (when the FirstView.xib is loaded) and is apparently managed while the initial tableView is filled with 7 cells, and is then released. So I need to identify where/how to make a retained reference to this controller. My Application Delegate should probably have some outlet that holds this controller that can be linked as the instance which is in the xib. Yeah, I'm new to this. I know I could just eliminate these troubles by avoiding the IB and doing things explicitly in code, but I figure I want to learn to use the IB flexibly.

最后:是的,我需要一个表视图控制器的保留实例。这听起来很简单,但是当我和IB一起工作时,这一点并不清楚。阅读我自己的帖子了解整个过程并进行修复。

Finally: Yes, I needed a retained instance of the table view controller. It sounds elementary, but this wasn't clear when working with the IB as I had. Read my own post for the whole process and fix.

除了:调试器需要详细的说明(感谢任何链接),或者它没有工作得很好。通过让App崩溃并阅读它生成的报告,我似乎可以更快地获得更多信息。但这需要一个乏味的终止,重新启动和3次点击。我真的想继续从这里开始连接输入,进行计算,并在每次更改时更新表格。这应该是困难的部分,而不是让框架成员工作的东西。

Aside: Either the debugger needs detailed instructions (any links appreciated), or it doesn't work very well. I seem to get more information more quickly by letting the App crash and reading the report it generates. But this requires a tedious termination, relaunch, and 3 clicks. I had really wanted to move on from this to wiring up the inputs, doing the calculation, and updating the table with each change. That's supposed to be the hard part, not this making a framework member work stuff.

进一步漫无边际:这就是iPhone SDK中的全部内容2.2.1。当时没有加入俱乐部以获得冷硬现金的iPhone OS 3.0非beta版尚未推出。我预计它将在WWDC 2009开放,但它实际上是今天(2009年7月17日),免费的公共3.0 SDK已经可用。

Further rambling: This was all in the iPhone SDK for 2.2.1. At the time iPhone OS 3.0 non-beta was not available yet without joining the club for cold hard cash. I expected it to be at the open of WWDC 2009, but it was actually today (July 17th 2009) that the free public 3.0 SDK was made available.

推荐答案

好的,如果您按照重现的步骤进行操作,我现在将添加解决此问题的步骤:

Okay so, if you followed the steps to reproduce I will now add the steps to solve this:

解决此问题的步骤


  1. 子类 UIViewController 。我打电话给我的 CombinationsViewController 。在此控制器中,为组合添加属性作为IBOutlet ViewController 来自下面的第6步。

  1. Subclass a UIViewController. I called mine CombinationsViewController. In this controller add a property as an IBOutlet for the combinationsTableViewController from step 6 below.

不要忘记导入正确的东西,并合成表视图控制器,也可以在 dealloc 方法中释放它。

Don't forget to import the right stuff, and synthesize the table view controller, also release it in the dealloc method.

FirstView.xib 文件的所有者类更改为这个最新的子类。

In FirstView.xib change the File's Owner class to this latest subclass.

链接它的combinationViewController在下面的步骤7中 FirstView.xib 中的组合表视图控制器的出口。

Link it's combinationsViewController outlet to the Combinations Table View Controller in the FirstView.xib made in step 7 below.

MainWindow.xib 中打开 TabBarController 选择第一个标签,并在Identity Inspector中将类更改为最新的子类( CombinationsViewController )。

Open the TabBarController in your MainWindow.xib select the first tab, and in the Identity Inspector change the class to the latest subclass (CombinationsViewController).

这使得表格正常填充并滚动内容。

That makes the table populate normally and scroll stuff.

现在我要继续前进并且g等一些自定义表视图单元格正在发生,实际上让我的应用程序做的事情。

Now I'm going to move on finally and get some custom table view cell stuff happening and actually make my app do stuff.

枚举重现的步骤作为修复的参考:


  1. 在Xcode中制作了一个新的标签栏应用程序。

  1. Made a new "Tab Bar Application" in Xcode.

打开标签视图控制器,将第一个标签的预制视图拖到 MainWindow.xib

Opened the Tab View Controller, dragged the prefab view for the first tab out into the MainWindow.xib

制作一个基于xib的新视图,名为 FirstView.xib ,作为给定<​​$ c $的模拟c> SecondView.xib ,并将该预制视图放入此xib。

Made a new view based xib called FirstView.xib as an analogue to the given SecondView.xib, and put that prefab view into this xib.

将视图链接到文件所有者视图 outlet。

Linked the view to the File's Owner's view outlet.

我修改了视图包含两个 UITextField 用于输入,以及一个 UITableView 用于输出。

I modified the view to contain two UITextFields for inputs, and a UITableView for output.

我将 UITableViewController 子类化为 CombinationsTableView 其中我填充了 NSArray 名为 combinati的属性ons 有11个字符串,然后添加 cell.text = [组合objectAtIndex:indexPath.row]; 代码,其中只有评论关于设置单元格。

I subclassed the UITableViewController as CombinationsTableView wherein I populated an NSArray property named combinations with 11 strings, and then added the cell.text = [combinations objectAtIndex:indexPath.row]; code where there had only been the comment about setting up the cell.

FirstView.xib 我添加了一个表视图控制器并设置它的类名以匹配这个新子类的名称,并在我的视图中将表视图控制拖到此组合表视图控制器两次。一旦链接 dataSource 和一次委托

In FirstView.xib I added a Table View Controller and set it's class name to match the name of this new subclass, and control-dragged the Table View in my view onto this Combinations Table View Controller twice. Once linking the dataSource and once the delegate.

此时表格会使用数据进行渲染,但滚动会中断。这是因为CombinationsTableView不会保留在任何位置。对于第一次IB用户来说,这是非常不清楚的。因此,您需要应用上面列出的修复。

At this point the table does render with data, but the scrolling breaks. This is because the CombinationsTableView isn't retained anywhere. And that's very unclear to a first time IB user. So you need to apply the fix listed above.

第一个在答案中对此进行总结的人会得到正确的答案复选标记。例如。创建一个viewController子类,它是FirstView.xib的文件所有者,并包含一个保留的IBOutlet,您可以在同一个xib文件中链接到表视图控制器。

The first person to summarize this in their answer get the correct answer check mark. E.G. Make a viewController subclass that is the file owner of FirstView.xib and contains an retained IBOutlet you can link to your table view controller in the same xib file.

这篇关于UITableView滚动崩溃App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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