[英]Element not showing up after UItableviewCell SubClass
我使用Custome uitableview单元格来避免性能下降。
这就是我填写表格单元格的方式:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = [NSString stringWithFormat:@"cell_%ld",(long)indexPath.row];
SubcategoryTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (sctvCell == nil) {
sctvCell= [[SubcategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
sctvCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
[sctvCell.label setText:appRecord.title];
return sctvCell;
}
在我的“ SubcategoryTableViewCell.h”标题中,并带有IBOutlet到单元格中的项目:
@property (weak, nonatomic) IBOutlet UILabel *mood_count_lbl;
在我的“ SubcategoryTableViewCell.m”类中:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
self.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,150)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
[whiteRoundedCornerView.layer setShadowColor:[UIColor grayColor].CGColor];
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.2;
[self.contentView addSubview:whiteRoundedCornerView];
[self.contentView sendSubviewToBack:whiteRoundedCornerView];
return self;
}
以上代码指出,除寄宿生和我的影子外
在客户单元中制作。
单元中的元素也已正确连接到客户单元。
问题回到我制作的动态“ reuseIdentifier”了吗?
如果我将其改回此代码,则无需定制单元格自定义和子类化,一切都可以正常工作:
NSString *reuseIdentifier = @"PlaceholderCell2";
UITableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (sctvCell == nil) {
sctvCell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
即使我这样更改代码:
self.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,150)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
[whiteRoundedCornerView.layer setShadowColor:[UIColor grayColor].CGColor];
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.2;
[self.contentView addSubview:whiteRoundedCornerView];
UILabel *_lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.contentView addSubview:_lblTitle];
[self.contentView sendSubviewToBack:whiteRoundedCornerView];
现在将显示新标签。
我正在检查单元格的名称和类,一切正常。
我的“ SubCategoryViewController.m”代码: http : //paste2.org/_nCY8zF9w
我的“ SubcategoryTableViewCell.m”代码: http : //paste2.org/_h9AJnzcV
我的“ SubcategoryTableViewCell.h”代码: http : //paste2.org/_vUJjEcXV
您正在将文本设置为sctvCell.label
,这是什么标签? 您还具有label
值为1的标签。要使用哪个标签?
假设您想将标签与标签以及函数initWithStyle:reuseIdentifier
,则应在此函数中添加标签,然后将文本设置为此标签。 reuseIdentifier
是动态的,应该是静态的。 您的单元只需要一个标识符。
假设您要使用IBoutlet
标签,那么您正在从xib或情节提要中加载单元格,就iOS 8而言,如果您在表中设置了带有reuseIdentifier的单元格,则从情节提要板上加载的单元格将始终具有值,因此可以不为零。 您可能想将自定义代码添加到单元格子类中的awakeFromNib
方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.