objective-c - 返回不兼容类型的变量

标签 objective-c ios cocoa-touch pointers ios-simulator

你好!

我尝试从该网站创建一个应用程序: http://www.appcoda.com/customize-table-view-cells-for-uitableview/

我们创建了一个包含食谱的表格,每个单元格中有 1 个食谱。我们总共有 16 个单元格/食谱。

我有麻烦了。当我尝试运行我的应用程序时,出现 2 个错误:

1)

2012-08-09 21:12:17.332 Simple table[8886:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/.../Library/Application Support/iPhone Simulator/5.1/Applications/1BA2DE48-7DEB-4564-BA33-A709412EB582/Simple table.app> (loaded)' with name 'SimpleTableCell''
*** First throw call stack:
(0x14b5022 0xeb5cd6 0x145da48 0x145d9b9 0x239638 0x23aeb7 0x23c8 0xb2c54 0xb33ce 0x9ecbd 0xad6f1 0x56d42 0x14b6e42 0x1d86679 0x1d90579 0x1d154f7 0x1d173f6 0x1da4160 0x16e84 0x17767 0x26183 0x26c38 0x1a634 0x139fef5 0x1489195 0x13edff2 0x13ec8da 0x13ebd84 0x13ebc9b 0x16c65 0x18626 0x1d1d 0x1c85)
terminate called throwing an exception(lldb) 

2)

Incompatible pointer types returning 'SimpleTableCell*_strong' from a function with result type 'UITableViewCell *'

此处指针单元格出现错误:

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

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];

    return cell;
}

完整文件 (Simple_tableViewController.m)

#import "Simple_tableViewController.h"
#import "SimpleTableCell.h"

@interface Simple_tableViewController ()

@end

@implementation Simple_tableViewController

{
    NSArray *tableData;
    NSArray *thumbnails;
    NSArray *prepTime;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

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

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Initialize table data
    tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

    // Initialize thumbnails
    thumbnails = [NSArray arrayWithObjects:@"egg_benedict.jpg", @"mushroom_risotto.jpg", @"full_breakfast.jpg", @"hamburger.jpg", @"ham_and_egg_sandwich.jpg", @"creme_brelee.jpg", @"white_chocolate_donut.jpg", @"starbucks_coffee.jpg", @"vegetable_curry.jpg", @"instant_noodle_with_egg.jpg", @"noodle_with_bbq_pork.jpg", @"japanese_noodle_with_pork.jpg", @"green_tea.jpg", @"thai_shrimp_cake.jpg", @"angry_birds_cake.jpg", @"ham_and_cheese_panini.jpg", nil];

    //initialize prep time
    prepTime=[NSArray arrayWithObjects:@"60", @"45", @"30", @"20", @"15", @"10", nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

为什么'cell'指针返回'SimpleTableCell*_strong'类型以及为什么会出现第一个错误?

非常感谢!

最佳答案

我对 xib 无法加载并不乐观,但我的猜测是您忘记在 SimpleTableCell.h 类定义中对 UITableViewCell 进行子类化。这可以解释为什么你得到不正确的返回值。这也可能解决您无法加载 Nib 的问题。

如果没有,请确保您的 nib 文件的名称与您在文件中加载的名称完全相同。拼写错误的文件可能会导致此问题。

关于objective-c - 返回不兼容类型的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11888713/

相关文章:

iphone - "error launching remote program: device locked out"xcode 4.2.1 和 ipad 5.0.1

objective-c - 检测屏幕/应用程序窗口的哪一部分正在使用 OpenGL?

objective-c - 使用内存中的 UIWebView 在 PhoneGap 中生成 PDF

ios - 在不了解代码的情况下发送应用程序

objective-c - 根据标签更改 UIButton 的颜色

objective-c - UILabel 的 backgroundColor 是不可动画的吗?

iphone - 当 UITextField 位于 UIScrollView 内时,使用点击手势关闭键盘时出现问题。

ios - UIPageControllerDelegate 操作顺序向后?

iphone - 如何从 iPhone 连接到 MySQL 数据库?

objective-c - 如何在不同的类中调用选择器