2011-10-03 71 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

相关问题