【问题标题】:iphone uitableview with custom cell - xcode 4带有自定义单元格的 iphone uitableview - xcode 4
【发布时间】:2011-11-30 05:00:19
【问题描述】:

好的,我知道网上有大量关于此的文档,我觉得我已经尝试了所有方法,但仍然无法正常工作。我正在尝试使用我在 IB 中创建的自定义单元格来实现表格视图。 CustomCell.xib 文件的所有者是UITableViewCell。这是我在其中实现表格视图的头文件:

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

我已将自定义单元格中的标签连接到DiscountCellController 中的standardLabeldiscountLabel。我收到此错误:

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

因为我错过了什么?

【问题讨论】:

  • 我认为你需要转换 DiscountCellController *cell = (DiscountCellController *)[tblView... 一件事。另外,你能分享一下 DiscountCellController 的代码吗?

标签: iphone xcode4 uitableview


【解决方案1】:

是的,你是。首先,这是一个常见的错误。在单元格的 nib 文件中,将文件的所有者定义为 NSObject。在您的 nib 文件中,您应该有一个 UITableViewCell,仅此而已。没有视野。将 UITableViewCell 的类型更改为 DiscountCellController。现在是重要的部分 - 右键单击​​ DiscountCellController 以创建指向标签等的链接。请勿从文件“所有者”中创建链接

【讨论】:

  • 我找到了这个很棒的教程。我删除了所有内容并从头开始。使用本教程作为基础,现在一切正常。 ijoshsmith.com/2011/07/16/…
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
相关资源
最近更新 更多