UITableView 作为 NIB 中的子视图的问题

发布于 2024-10-04 00:28:22 字数 4287 浏览 2 评论 0原文

执行摘要:我正在尝试使用 UITableView 作为窗口“主”视图的子视图;它出现了,但是当我滚动窗口时,我收到以下错误消息:

2010-11-20 17:17:51.958 xwtf[6997:207] -[NSCFString tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x5f46550

我有一个 ViewController 的子类,其中包含由我的应用程序委托加载的相应 NIB 文件。这是典型的新项目模板。在该 NIB 文件中,我添加一个 UITableView 作为视图的子视图。在 Interface Builder 中,我将其调整大小以占据整个窗口(即它完全重叠其父视图)。

现在我需要指定数据源和委托。我创建了一个名为 DumbTableHelper 的类,如下所示:

@interface DumbTableHelper : NSObject<UITableViewDelegate, UITableViewDataSource> {
}

所以,它不是 UIViewController 的子类,但我试图保持简单 - 我需要的只是一个委托和它可以调用的数据源,对吧?除了dealloc 之外,我在其.m 文件中的方法是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
  return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"DumbTableCell";
  UITableViewCell *cell = [tableView
                           dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                     reuseIdentifier:CellIdentifier]
            autorelease];
  }
  return cell;
}

如您所见,我什至没有配置任何单元格;我只指定 10 行,这足以强制滚动。无论如何,在 Interface Builder 中,我将一个对象从库拖到我的 NIB 中。然后,我将其类名称更改为 DumbTableHelper 并将其指定为已存在的 UITableView 的数据源和委托。当我运行它时,会出现该表。当我尝试在表格中向下滚动时,我得到:

010-11-20 18:19:54.673 xwtf[6997:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x5f46550'
*** Call stack at first throw:
(
0   CoreFoundation                      0x0259eb99 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x0239340e objc_exception_throw + 47
2   CoreFoundation                      0x025a06ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3   CoreFoundation                      0x025102b6 ___forwarding___ + 966
4   CoreFoundation                      0x0250fe72 _CF_forwarding_prep_0 + 50
5   UIKit                               0x00321d6f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 619
6   UIKit                               0x00317e02 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
7   UIKit                               0x0032c69f -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1348
8   UIKit                               0x003247ec -[UITableView layoutSubviews] + 242
9   QuartzCore                          0x0455c481 -[CALayer layoutSublayers] + 177
10  QuartzCore                          0x0455c1b1 CALayerLayoutIfNeeded + 220
11  QuartzCore                          0x045552e0 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 302
12  QuartzCore                          0x04555040 _ZN2CA11Transaction6commitEv + 292
13  QuartzCore                          0x04585ebb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
14  CoreFoundation                      0x0257ff4b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
15  CoreFoundation                      0x02514b27 __CFRunLoopDoObservers + 295
16  CoreFoundation                      0x024ddce7 __CFRunLoopRun + 1575
17  CoreFoundation                      0x024dd350 CFRunLoopRunSpecific + 208
18  CoreFoundation                      0x024dd271 CFRunLoopRunInMode + 97
19  GraphicsServices                    0x02d5d00c GSEventRunModal + 217
20  GraphicsServices                    0x02d5d0d1 GSEventRun + 115
21  UIKit                               0x002beaf2 UIApplicationMain + 1160
22  xwtf                                0x00001af4 main + 102
23  xwtf                                0x00001a85 start + 53
)

所以它正在调用 tableView:cellForRowAtIndexPath: ......字符串?有人有什么想法吗?

如果这是一个愚蠢的问题,我很抱歉 - 到目前为止,我所有的表格视图都是由 UITableViewController 实例以编程方式创建的,但现在我需要添加一个 UITableView 作为子视图,不占据整个屏幕。我已经阅读了 Apple 关于表格视图和开始 iPhone 开发的指南,以确保我没有遗漏任何内容,但无济于事。

谢谢!

The executive summary: I'm trying to use a UITableView as a subview of my window's "main" view; it appears, but when I scroll the window I get the following error message:

2010-11-20 17:17:51.958 xwtf[6997:207] -[NSCFString tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x5f46550

I have a subclass of ViewController with corresponding NIB file that is loaded by my application delegate. It's your typical New Project template. In that NIB file, I add a UITableView as a subview of view. In Interface Builder, I size it to take up the entire window (i.e. it completely overlaps its parent view).

Now I need to specify a data source and delegate. I create a class named DumbTableHelper like so:

@interface DumbTableHelper : NSObject<UITableViewDelegate, UITableViewDataSource> {
}

So, it's not a subclass of UIViewController, but I'm trying to keep this simple -- all I need is a delegate and data source it can call out to, right? Aside from dealloc, my methods in its .m file are:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
  return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"DumbTableCell";
  UITableViewCell *cell = [tableView
                           dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                     reuseIdentifier:CellIdentifier]
            autorelease];
  }
  return cell;
}

As you can see, I'm not even configuring any cell; I just specify 10 rows, which is enough to force scrolling. Anyway, in Interface Builder I drag an object from the library into my NIB. I then change its class name to DumbTableHelper and specify it as the data source and delegate of the UITableView already there. When I run it, the table appears. When I try to scroll down in the table, I get:

010-11-20 18:19:54.673 xwtf[6997:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x5f46550'
*** Call stack at first throw:
(
0   CoreFoundation                      0x0259eb99 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x0239340e objc_exception_throw + 47
2   CoreFoundation                      0x025a06ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3   CoreFoundation                      0x025102b6 ___forwarding___ + 966
4   CoreFoundation                      0x0250fe72 _CF_forwarding_prep_0 + 50
5   UIKit                               0x00321d6f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 619
6   UIKit                               0x00317e02 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
7   UIKit                               0x0032c69f -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1348
8   UIKit                               0x003247ec -[UITableView layoutSubviews] + 242
9   QuartzCore                          0x0455c481 -[CALayer layoutSublayers] + 177
10  QuartzCore                          0x0455c1b1 CALayerLayoutIfNeeded + 220
11  QuartzCore                          0x045552e0 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 302
12  QuartzCore                          0x04555040 _ZN2CA11Transaction6commitEv + 292
13  QuartzCore                          0x04585ebb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
14  CoreFoundation                      0x0257ff4b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
15  CoreFoundation                      0x02514b27 __CFRunLoopDoObservers + 295
16  CoreFoundation                      0x024ddce7 __CFRunLoopRun + 1575
17  CoreFoundation                      0x024dd350 CFRunLoopRunSpecific + 208
18  CoreFoundation                      0x024dd271 CFRunLoopRunInMode + 97
19  GraphicsServices                    0x02d5d00c GSEventRunModal + 217
20  GraphicsServices                    0x02d5d0d1 GSEventRun + 115
21  UIKit                               0x002beaf2 UIApplicationMain + 1160
22  xwtf                                0x00001af4 main + 102
23  xwtf                                0x00001a85 start + 53
)

So it's calling tableView:cellForRowAtIndexPath: on a.. String? Anyone have any ideas?

I'm sorry if this turns out to be a stupid question -- so far all my table views have been created programmatically by UITableViewController instances, but now I need to add a UITableView as a subview, which doesn't take up the entire screen. I've read the Apple guide on table views and Beginning iPhone Development to make sure I'm not missing anything, but to no avail.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满天都是小星星 2024-10-11 00:28:22

好的,我已经弄清楚原因了:我的 ViewController 子类(同时也是我的 NIB 文件中的文件所有者)没有声明 DumbTableHelper 的出口。 Nib 文件资源编程指南位于 http:// /developer.apple.com/library/mac/documentation/cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html 说:

nib 文件中的对象创建时保留计数为 1,然后自动释放...如果为 nib 文件对象定义出口,则应始终定义一个 setter 方法(或声明的属性)来访问该出口。出口的 setter 方法应保留其值,包含顶级对象的出口的 setter 方法必须保留其值以防止它们被释放。

当表格首次显示时,DumbTableHelperretainCount 仍然为 1,并且尚未自动释放,因此它可用于填充最初显示的行。然而,当我滚动时,它不再存在于内存中。从堆栈跟踪中,我们可以推测 DumbTableHelper 原来所在的内存地址已经分配了一个字符串,因此它错误地收到了 tableView:cellForRowAtIndexPath: 消息。

同样,只需将文件所有者的插座连接到 Interface Builder 文件中的 DumbTableHelper 实例即可解决该问题。

Okay, I've figured out why: My subclass of ViewController that also serves as the File Owner in my NIB file has no declared outlet to DumbTableHelper. The Resource Programming Guide on Nib files at http://developer.apple.com/library/mac/documentation/cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html says:

Objects in the nib file are created with a retain count of 1 and then autoreleased... If you define outlets for nib-file objects, you should always define a setter method (or declared property) for accessing that outlet. Setter methods for outlets should retain their values, and setter methods for outlets containing top-level objects must retain their values to prevent them from being deallocated.

When the table is first displayed, the DumbTableHelper still has a retainCount of 1 and has not yet been autoreleased, so it can be used to populate the rows that are initially displayed. When I scroll, however, it no longer exists in memory. From the stack trace, we can surmise that a string has been allocated in the memory address where the DumbTableHelper used to be, and so it errantly receives the tableView:cellForRowAtIndexPath: message.

Again, simply connecting an outlet from the File Owner to the DumbTableHelper instance in Interface Builder file solves the problem.

三生一梦 2024-10-11 00:28:22

我建议删除 Interface Builder 中的连接并尝试重新建立。看起来数据源插座似乎已连接到笔尖中的 NSString。您还可以尝试使用 UITableView 的委托和数据源属性在代码中建立连接。

I suggest deleting your connections in Interface Builder and trying to re-establish. It certainly seems as if the dataSource outlet has been connected to an NSString in the nib. You could also just try establishing the connection in code, using the delegate and datasource properties of UITableView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文