【问题标题】:creating custom UITableViewCell?创建自定义 UITableViewCell?
【发布时间】:2013-08-29 17:30:39
【问题描述】:

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

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

AcSummaryCellUITableViewCell 的子类,它有一个名为 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 的回答,我正在延迟连接到文件所有者,这是错误的,而是我必须延迟连接到自定义单元格。但是当我尝试这个时,我没有得到我迟到了,而不是我越来越喜欢这张图片-

现在的问题是如何将我的 outlate 与自定义单元格连接起来? 这是我的 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

【问题讨论】:

  • 您是否将xib中的自定义类设置为ACSummayCell?
  • 是的,我做到了。并尝试删除并重新创建连接/xib 等。但没有运气。
  • 检查一切是否连接良好,或者您的 xib 文件中没有错误的引用。
  • 检查acLabel是否正确连接?
  • 我这样做了,甚至重新创建了我的 UITabelViewCell 子类和 xib.. 对我来说奇怪的是,我正在 AcSummaryCell 中创建连接并为 ACSummaryVC 获取错误。

标签: ios objective-c uitableview


【解决方案1】:

我看到了你的错误:

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

要连接到 IBOutlet,只需选择编辑器(显示助手编辑器)-> 然后选择自动-AcSummaryCell.h -> 然后拖动并连接所需的 IBOutlet。

【讨论】:

    【解决方案2】:

    正确遵循此代码....

    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 与子类链接....

    【讨论】:

    • 我也是这样做的。
    【解决方案3】:

    【讨论】:

    • 感谢 Mayank,我已经在关注该教程,但没有得到“连接指向您的自定义单元格的插座”的内容..当我右键单击笔尖中的自定义单元格以与 outlate 连接时它没有显示我的约会对象?
    【解决方案4】:

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

    【讨论】:

    • 你能解释一下吗,因为当我尝试连接到自定义单元格时,它没有显示我的连接时间,而是显示了附件视图、背景视图等。
    • 检查您在 XIB 的身份检查器中选择的类。它应该是您的自定义 tableviewcell 的类名(在您的情况下为 AcSummayCell)。单击 XIB 中的 File's Owner 并打开身份检查器并将类名称更改为 AcSummayCell。
    • 谢谢你救了我。我是为文件所有者这样做的。现在它可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2011-12-13
    • 2014-04-12
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多