xcode 无法为自定义 UITableViewCell 连接 IBOutlets
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6433976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't connect IBOutlets for custom UITableViewCell
提问by blabus
I've got a problem trying to create a custom tableview cell for my iPhone app in Xcode 4. I've created the .h and .m files, created IBOutlets for all of the subviews that should be present inside the cell, and dragged the actual controls onto the cell's XIB file in Interface Builder. However, I can't figure out how to connect the outlets to the IB controls. Every tutorial I read says to control-drag from the TableViewCell object in the objects list to the subviews, but when I do that my IBOutlets are not listed in the popup window. The only outlets that are available are 'acccessoryView', 'backgroundView', 'editingAccessoryView' and 'backgroundSelectedView'. Shouldn't I also be seeing my own IBOutlets in that list as well? Or am I not following the correct procedure? (I'm pretty new to iPhone development).
我在尝试在 Xcode 4 中为我的 iPhone 应用程序创建自定义 tableview 单元格时遇到问题。我创建了 .h 和 .m 文件,为应该存在于单元格内的所有子视图创建了 IBOutlets,然后拖动在 Interface Builder 中单元的 XIB 文件上的实际控件。但是,我不知道如何将插座连接到 IB 控件。我读过的每个教程都说要从对象列表中的 TableViewCell 对象控制拖动到子视图,但是当我这样做时,我的 IBOutlets 没有列在弹出窗口中。唯一可用的出口是“acccessoryView”、“backgroundView”、“editingAccessoryView”和“backgroundSelectedView”。我不应该也在该列表中看到我自己的 IBOutlets 吗?还是我没有遵循正确的程序?(一世'
Here is the code I'm using for the custom cell class:
这是我用于自定义单元格类的代码:
.h :
。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 :
.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
(Note that I reduced the subviews to just the 'artwork' image view for now, to simplify figuring it out). Thanks for any help!
(请注意,我现在将子视图缩减为“艺术品”图像视图,以简化计算)。谢谢你的帮助!
回答by Patrick Perini
Unless I'm misunderstanding:
除非我误解:
Try adding the IBOutlet tag.
尝试添加 IBOutlet 标签。
IBOutlet UIImageView *artwork;
and
和
@property (nonatomic, retain) IBOutlet UIImageView *artwork;
This tag is a #define within Xcode that tells Interface Builder to recognize your code.
这个标签是 Xcode 中的 #define,它告诉 Interface Builder 识别你的代码。
回答by blabus
You have to set the Class Identity of the table view cell in Interface Builder to your custom class, even if the .xib
file is named the same as your custom class you still need to explicitly mention that File's Owner is an object of type RecommendationCell.
您必须将 Interface Builder 中表格视图单元格的 Class Identity 设置为您的自定义类,即使.xib
文件与您的自定义类命名相同,您仍然需要明确提及 File's Owner 是一个 RecommendationCell 类型的对象。
The other issue (mentioned by Patrick) is that you should declare an IBOutlet
in your .h on the @property
declaration, not the instance variable, like so:
另一个问题(由帕特里克提到)是你应该IBOutlet
在你的 .h声明中声明一个@property
,而不是实例变量,如下所示:
UIImageView *artwork;
@property (nonatomic, retain) IBOutlet UIImageView *artwork;
回答by Christopher Larsen
Wanted to update since I came across this recently. In xcode 7.1.1 you can't drag from the file's owner like you usually can for UIViewController xib's.
想更新,因为我最近遇到了这个。在 xcode 7.1.1 中,您不能像通常为 UIViewController xib 那样从文件所有者拖动。
Go to the .xib file for your custom UITableViewCell in interface builder.
转到界面生成器中自定义 UITableViewCell 的 .xib 文件。
This is hard to explain, so I've included an image, even with it I'm not sure I'm really explaining this well, but hopefully it helps. Or at least if I come across it again I'll know what I'm talking about.
这很难解释,所以我已经包含了一张图片,即使有它我也不确定我是否真的很好地解释了这一点,但希望它有所帮助。或者至少如果我再次遇到它,我会知道我在说什么。
Right Click on the custom uitableviewcell containing your UILabels or UIImage etc
右键单击包含您的 UILabels 或 UIImage 等的自定义 uitableviewcell
This will bring up a menu listing all of your IBOutlets and objects contained within your custom uitableviewcell. Put your mouse over the empty circle on the right and ctrl-click-drag to your object.
这将打开一个菜单,其中列出了您的自定义 uitableviewcell 中包含的所有 IBOutlets 和对象。将鼠标放在右侧的空圆圈上,然后按住 ctrl 单击并拖动到您的对象。