2012-08-09 93 views
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'類型,爲什麼會出現第一個錯誤?

非常感謝!

回答

1

我對xib無法被加載並不積極,但我的猜測是您忘記了在您的SimpleTableCell.h類定義中繼承UITableViewCell。這將解釋爲什麼你得到不正確的返回值。這可能會解決您無法加載筆尖問題。

如果沒有,請確保您的nib文件與您在文件中加載的命令完全相同。拼寫錯誤的文件可能會導致此問題。

+0

這是我SimpleTableCell.h源文件: '... // @接口SimpleTableCell:UITableViewCell的...' 我的意思是我定義的UITableViewCell作爲父類SimpleTableCell類的。對? – Vladimir 2012-08-09 19:53:20

+0

我已經定義了一個子類,但沒有奏效。可能是其他原因? – Vladimir 2012-08-09 21:33:12