0

你好!

我尝试从该网站创建一个应用程序: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' 类型以及为什么会出现第一个错误?

非常感谢!

4

1 回答 1

1

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

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

于 2012-08-09T17:40:05.110 回答