4

我在尝试在 Xcode 4 中为我的 iPhone 应用程序创建自定义 tableview 单元格时遇到问题。我创建了 .h 和 .m 文件,为单元格内应该存在的所有子视图创建了 IBOutlets,然后拖动Interface Builder 中单元格的 XIB 文件中的实际控件。但是,我不知道如何将插座连接到 IB 控件。我阅读的每个教程都说要从对象列表中的 TableViewCell 对象控制拖动到子视图,但是当我这样做时,我的 IBOutlets 不会在弹出窗口中列出。唯一可用的插座是“acccessoryView”、“backgroundView”、“editingAccessoryView”和“backgroundSelectedView”。我不应该也在该列表中看到我自己的 IBOutlets 吗?还是我没有遵循正确的程序?(一世'

这是我用于自定义单元类的代码:

。H :

//
//  RecommendationCell.h
//  Tuneplug
//
//  Created by Bill Labus on 6/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface RecommendationCell : UITableViewCell {
    UIImageView *artwork;
}

@property (nonatomic, retain) UIImageView *artwork;

@end

.m:

//
//  RecommendationCell.m
//  Tuneplug
//
//  Created by Bill Labus on 6/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RecommendationCell.h"

@implementation RecommendationCell

@synthesize artwork;


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

- (void)dealloc
{
    [super dealloc];
}

@end

(请注意,我现在将子视图简化为“艺术品”图像视图,以简化计算)。谢谢你的帮助!

4

3 回答 3

6

除非我误解:

尝试添加 IBOutlet 标签。

IBOutlet UIImageView *artwork;

@property (nonatomic, retain) IBOutlet UIImageView *artwork;

这个标签是 Xcode 中的一个#define,它告诉 Interface Builder 识别你的代码。

于 2011-06-22T01:46:58.380 回答
2

您必须将 Interface Builder 中表格视图单元格的 Class Identity 设置为您的自定义类,即使该.xib文件的名称与您的自定义类相同,您仍然需要明确提及 File's Owner 是 RecommendationCell 类型的对象。

另一个问题(Patrick 提到)是您应该在声明IBOutlet中的 .h 中声明 an @property,而不是实例变量,如下所示:

UIImageView *artwork;

@property (nonatomic, retain) IBOutlet UIImageView *artwork;
于 2011-06-22T01:49:19.937 回答
0

想更新,因为我最近遇到了这个。在 xcode 7.1.1 中,您不能像通常对 UIViewController xib 那样从文件所有者中拖动。

在界面生成器中转到自定义 UITableViewCell 的 .xib 文件。

在此处输入图像描述

这很难解释,所以我附上了一张图片,即使有它我也不确定我是否真的很好地解释了这一点,但希望它有所帮助。或者至少如果我再次遇到它,我会知道我在说什么。

右键单击包含您的 UILabels 或 UIImage 等的自定义 uitableviewcell

这将打开一个菜单,列出您的自定义 uitableviewcell 中包含的所有 IBOutlets 和对象。将鼠标放在右侧的空圆圈上,然后按住 ctrl-click-drag 到您的对象。

于 2015-12-02T02:45:56.807 回答