2011-10-03 69 views
1

好吧,所以我知道有關於此的網上文檔噸,我覺得我已經嘗試了一切,仍然無法讓它工作。我正在嘗試使用我在IB中創建的自定義單元來實現tableview。文件的CustomCell.xib文件的所有者是UITableViewCell。這是我在實現表視圖頭文件:iphone uitableview與自定義單元格 - xcode 4

#import <UIKit/UIKit.h> 

@interface QuickCalcController : UIViewController<UITabBarDelegate, UITableViewDelegate, UITableViewDataSource>{ 

NSMutableArray *numbers; 
NSMutableArray *discount_numbers; 

} 

@property (nonatomic, retain) IBOutlet UITableView *tblView; 

@end 

,這裏是在執行文件中的代碼:

- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"DiscountCellController"; 
    DiscountCellController *cell = [tblView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil){ 
    NSLog(@"New Cell Made"); 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DiscountCellController" owner:nil options:nil]; 

    for(id currentObject in topLevelObjects) 
    { 
     if([currentObject isKindOfClass:[DiscountCellController class]]) 
     { 
      cell = (DiscountCellController *)currentObject; 
      break; 
     } 
    } 
} 


cell.standardLabel.text = @"hi";//[discount_numbers objectAtIndex:indexPath.row]; 
cell.discountLabel.text = @"hi";//[discount_numbers objectAtIndex:indexPath.row]; 
NSLog(@"setting the cell"); 
return cell; 
} 

#pragma mark - 
#pragma mark Table view data source 

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

- (NSString *)tableView:(UITableView *)tblView titleForHeaderInSection: (NSInteger)section 
{ 
    return nil; 
} 

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

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tblView { 
    return nil; 
} 

我已經在自定義單元格連接標籤到standardLabeldiscountLabelDiscountCellController。我得到這個錯誤:

[3390:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x4e227d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key standardLabel.' 

因爲我錯過了什麼?

+0

我想你需要投DiscountCellController * cell =(DiscountCellController *)[ tblView ... for one thing。另外,你可以分享DiscountCellController的代碼嗎? – Jim

回答

3

是的,你是。首先,這是一個常見的錯誤。在單元格的nib文件中,將File的所有者定義爲NSObject。在你的nib文件中,你應該有一個UITableViewCell,就是這樣。沒有看法。將UITableViewCell的類型更改爲DiscountCellController。現在的重要組成部分 - 右鍵單擊​​DiscountCellController,使鏈接到您的標籤等不要從文件的鏈接「S所有者

+1

我發現這個GREAT教程,我刪除了所有內容,並從頭開始使用本教程作爲基礎,現在一切正常。 ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/ – coder

相關問題