Xcode 4,基于视图的应用程序UITableView 向上滚动时崩溃

发布于 2024-12-05 00:43:10 字数 1503 浏览 1 评论 0原文

在 Xcode 中,使用基于视图的应用程序模板启动一个新项目(仅限 iPhone)。

在带有 Interface Builder 的视图 (ProjectNameViewController.xib) 的 xib 中,添加一个 TableView。

创建一个新的 UITableView 的 Objective-C 类子类,在头文件中添加 UITableViewDataSource 协议并添加此属性:

NSArray *data;

在实现文件中实现 2 个必需的方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    cell.textLabel.text = [data objectAtIndex:indexPath.row];

    return cell;
}

在 init 方法中添加此:

data = [[NSArray arrayWithObjects:@"First", @"Second", @"Third", nil] retain];

返回 Interface Builder,添加一个新对象与之前的自定义类(身份检查器)。 在 UITableView 中(在 Connections 检查器上的 Interface Builder 中)将出口数据源添加到前一个对象。

运行应用程序,它显示带有数据的表,然后当您向上滚动时,它在主线程上崩溃并显示信号:“EXC_BAD_ACCESS”(没有抛出异常)。

我对带有 Cocoa 框架的 Xcode + Interface Builder 几乎不陌生,我试图理解 Movel-View-Controller 来创建具有可重用代码的应用程序,但我不明白如何划分对象来为每个对象创建控制器应用程序中的 UI 元素,因为在这种情况下,如果我将方法放在由文件所有者子类化的类上(协议与上面的类相同),那么一切都会起作用。 但文件所有者的类是 ViewController,我希望 TableView 与应用程序中的任何其他对象分开。如果这不是构建应用程序的方式(推荐方式),请告诉我。

提前致谢。

In Xcode start a new project with the View-based Application template (iPhone only).

In the xib for the view (ProjectNameViewController.xib) with Interface Builder, add a TableView.

Create a new Objective-C class subclass of UITableView, in the header file add the UITableViewDataSource protocol and add this attribute:

NSArray *data;

In the implementation file implement the 2 required methods:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    cell.textLabel.text = [data objectAtIndex:indexPath.row];

    return cell;
}

In the init method add this:

data = [[NSArray arrayWithObjects:@"First", @"Second", @"Third", nil] retain];

Back to Interface Builder, add a new object with the previous custom class (Identity inspector).
In the UITableView (in Interface Builder on the Connections inspector) add the outlet dataSource to the previous object.

Run the application, it show the table with the data then when you scroll up it crashed with signal: "EXC_BAD_ACCESS" on the main-thread (no exception thrown).

I'm barely new to Xcode + Interface Builder with the Cocoa Framework, I'm trying to understand the Movel-View-Controller for create apps that have reusable code but I can't understand how I can divide objects for make controllers for each UI element in the application, because in this case if I put the methods on the class sub-classed by the File's Owner (with the protocol in the same way of the class above) it all works.
But the class of the File's Owner is the ViewController and I want the TableView separate from any other object in the application. If this is not the way for build an application (the recommended way), tell me.

Thanks in advance.

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

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

发布评论

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

评论(1

遮云壑 2024-12-12 00:43:10

问题解决了。

在 ViewController 中添加 2 个 IBOutlet:

一个用于 UITableView,一个用于 TableViewController(示例):

@interface ViewBasedAppViewController : UIViewController {

    IBOutlet UITableView *table;
    IBOutlet TableViewController *tableController;
}

TableViewController 是一个 NSObject。
在 Interface Builder 中连接插座:
从文件所有者到表选择“表”;
从文件的所有者到TableViewController对象选择“tableController”;

Problem solved.

In the ViewController add 2 IBOutlet:

One for the UITableView and one for the TableViewController (example):

@interface ViewBasedAppViewController : UIViewController {

    IBOutlet UITableView *table;
    IBOutlet TableViewController *tableController;
}

The TableViewController is a NSObject.
In Interface Builder connect the outlets:
From the File's owner to the table select "table";
From the File's owner to the TableViewController object select "tableController";

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