创建自定义UITableViewCell?

时间:2013-08-27 05:15:04

标签: ios objective-c uitableview

我知道这个问题在这里一次又一次地被问到,但没有找到任何解决我的问题的方法。我正在通过xib创建一个自定义UITableViewCell并尝试加载它。我的VC名为 ACSummaryVC

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"SimpleTableCell";

    AcSummaryCell *cell = (AcSummaryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AcSummaryCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
}

AcSummaryCell UITableViewCell的子类,它有一个名为acLabel的UILabel IBOutlate。当我编译项目后,我得到以下错误 -

`Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ACSummaryVC 0x71465c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key acLabel.'`

我在AcSummayCell类中创建了acLabel的连接,但是我从ACSummaryVC收到错误?我做错了什么?

编辑: - 根据Mani的回答,我正在连接到文件所有者,这是错误的而是我要连接到自定义单元格。但是当我尝试这个我没有得到我与我联系,但我得到这样的形象 - enter image description here

现在的问题是如何将我的支出与自定义单元格连接? 这是我的AcSummayCell.h

@interface AcSummayCell : UITableViewCell
@property(nonatomic, retain)IBOutlet UILabel* acLabel;
@property(nonatomic, retain)IBOutlet UILabel* namelbl;
@property(nonatomic, retain)IBOutlet UILabel* cBalancelbl;
@property(nonatomic, retain)IBOutlet UILabel* avBalancelbl;

和AcSummayCell.m

#import "AcSummayCell.h"

@implementation AcSummayCell
@synthesize acLabel = _acLabel;
@synthesize namelbl = _namelbl;
@synthesize cBalancelbl = _cBalancelbl;
@synthesize avBalancelbl = _avBalancelbl;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

4 个答案:

答案 0 :(得分:5)

您设置了错误的插座连接。这是因为您已将标签的插座连接到文件的所有者。从文件所有者中删除该标签插座连接,并连接指向自定义单元的插座。它应该工作。

答案 1 :(得分:1)

答案 2 :(得分:1)

正确遵循此代码....

AudioCell.h

#import<UIKit/UIKit.h>

@interface AudioCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *artistLabel;
...


@end

AudioCell.m

#import "AudioCell.h"

@implementation AudioCell

@synthesize titleLabel = _titleLabel, artistLabel = _artistLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

现在在TableView数据源的类中实现此代码....

#import "AudioCell.h"
.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"AudioCell";

AudioCell *cell = (AudioCell *)[self.audioFeedTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = (AudioCell *)[nibArray objectAtIndex:0];
    [cell configurePlayerButton];
}

// Configure the cell..


cell.titleLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.artistLabel.text = [commentsArray objectAtIndex:indexPath.row];
....

return cell;
}

以相同的方式将自定义TableViewCell与子类链接.... enter image description here

答案 3 :(得分:0)

我看到你的错误:

您没有在customCell中指定Cell类和重用标识符 首先创建自定义单元格AcSummaryCell:UITableViewCell,然后在nib单元格中为tableCell选择类。还在NIB中分配小区标识符。如果正确,则必须显示您的单元格 对象字段中的AcSummaryCell-SimpleTableCell

对于IBOutlet的连接,只需选择编辑器(show assistant editor) - &gt;然后选择自动 - AcSummaryCell.h - &gt;然后拖动和连接你想要的IBOutlet。

相关问题