objective-c - xcode 4.2 将 View Controller 类设置为自定义类错误

标签 objective-c ios xcode4.2

我正在尝试学习 iOS 开发,但我无法弄明白! 例子: 我使用 Xcode 4.2 创建了一个新的“单 View 应用程序”。然后我向 View 添加一个 TableView 。然后我去 File>New>New File 并创建一个 UIViewControllerSubclass,它是 UITableViewController 的子类。 然后,我单击 Storyboard 中唯一的 View Controller ,进入身份检查器下方并将其类更改为我刚刚创建的类。然后我链接 TableView 数据源并委托(delegate)给 View Controller 。我打开我创建的类中的 .m 文件,并将其节数设置为 1,节中的行数设置为 3,然后“配置”单元格以说“Hello”(cell.textlabel.text=@"Hello “;)。当我运行该程序时,我收到一条错误消息“SIGABRT”,但该程序中根本没有任何警告或错误。我做错了什么?

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

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

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

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

    cell.textLabel.text=@"cell1";
    return cell;
}

最佳答案

当您第一次创建项目时,转到 Storyboard并删除 XCode 默认为您创建的通用 View Controller 场景。然后将一个 Table View Controller 对象从对象浏览器拖到您的 Storyboard上,并在身份检查器中将类更改为您的自定义类。 (请注意,您拖出的 TableView Controller 将已经包含您需要的 tableView 和原型(prototype)单元格。)像以前一样连接数据源和委托(delegate)属性,并在您的自定义类 .m 文件中实现上述代码。

之后您将看不到错误。

关于objective-c - xcode 4.2 将 View Controller 类设置为自定义类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9639664/

相关文章:

iphone - 如何将相框添加到 UIImage?

iphone - iPad App - 以编程方式更改语言

iphone - 在 Xcode 4.2 for iOS 项目中安装 Core-Plot

ios - #import"PGPlugin.h""file not found"xcode4.2 上的 phonegap2.0 错误

iphone - 在iOS应用程序中添加自定义表情

ios - 重新加载 UIWebView

iphone - 智能 CVS 和核心数据

ios - 以编程方式调整 Storyboard 中 UITableView 的 y 值

ios - (iOS) 如何让 UIPickerView 中的第一个值不可选?

iphone - 使用 Web 服务的教程?