[英]Adding subview to UITableViewCell content view
如何从实现文件中添加一个subView
到自定义UITableViewCell
,即drawRect
方法中的MyCustomCell.m
? 而不是在视图控制器中的cellForRowAtIndexPath
方法中添加它...
我试过[self.contentView addSubView: view1];
但它给出了这个警告: UIView may not respond to -addSubView:
编辑:
导致崩溃的代码是:
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code (add your view here.)
[self.contentView addSubView: self.view1];
}
return self;
}
这是你的错误拼写; 不是addSubView:
而是addSubview:
.
你的问题是你正在调用错误的方法。 该方法不是addSubView:
而是addSubview:
. 您可以在Apple文档页面上查找: http : //developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
你可以用这个......
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code (add your view here.)
}
return self;
}
在这里你可以添加你的子视图..编辑:
我使用它来制作我的自定义单元格,它工作正常.... http://blog.webscale.co.in/?p=284
看起来self.view1
尚未初始化。 您应该稍后添加它。 我要做的是,在cellForRowAtIndexPath:(NSIndexPath*)indexPath
方法中,在单元格初始化之后,调用一个设置单元格的方法,添加所需的视图或您需要的任何其他元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.