iphone uitableview with custom cell - xcode 4

2019-07-25 14:00发布

问题:

Ok so I know there is TONS of documentation online about this, and I feel like I have tried everything, and still can't get it to work. I am trying to implement a tableview with a custom cell that I created in the IB. The file's owner for the CustomCell.xib file is UITableViewCell. This is the header file I am implementing the table view in:

#import <UIKit/UIKit.h>

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

NSMutableArray *numbers;
NSMutableArray *discount_numbers;

}

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

@end

and here is the code in the implementation file:

- (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; 
}

I have connected the labels in the custom cell to the standardLabel and discountLabel in DiscountCellController. I get this error:

[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.'

As I missing something?

回答1:

Yes, you are. First of all, this is a common error. In your nib file for the cell, define File's owner as NSObject. In your nib file, you should have a UITableViewCell, that's all. No view. Change the type of the UITableViewCell to DiscountCellController. Now the important part - right click the DiscountCellController to make the links to your labels, etc. DO NOT MAKE THE LINKS FROM THE FILE"S OWNER