为什么我的单元格在我的UITableView中显示为空白?
我对我的UITableView
有点奇怪的结构。基本上,我有构成表的ArticleCell
对象(UITableViewCell
的子类),并且每个ArticleCell
由“前”视图和“后”视图组成。前视图包含所有标签以及用户看不到的内容,而后面的图标则左右两个。这两个视图的想法是这样的,用户可以将顶部视图向右或向左滑动以快速选择一个选项(有点像在Reeder中)。为什么我的单元格在我的UITableView中显示为空白?
我已经在故事板中稍微实现了这一点,但主要是在代码中。我在故事板上做的唯一一件事是完成了UITableViewController
的布局,并且我为原型单元格命名了标识符(标识符:“ArticleCell”)。
除了故事板,正如我所说的,一切都是在代码中完成的。我从Core Data获取单元格的信息,并通过将单元格设置为单元格的article
属性构建单元格,然后将该文章设置为CellFront
UIView
的article
属性。由于存在两种单元格布局,取决于单元格所保持的文章类型,因此在CellFront
中它会检查单元格的类型(名为isUserAddedText
的属性)并相应地创建布局。
但正如我所说,应用程序加载时,没有任何显示,所有单元格加载,我可以点击它们去他们的内容,但单元格本身是空白的。
相关的代码如下:
单元数据源:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ArticleInfo *articleInfo = [self.fetchedResultsController objectAtIndexPath:indexPath];
static NSString *CellIdentifier = @"ArticleCell";
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.article = articleInfo;
return cell;
}
在ArticleCell.m
,我推翻文章的集合方法,以便当上述数据源的方法调用它,它可以设置视图的article
财产也是如此。
- (void)awakeFromNib {
[super awakeFromNib];
self.cellBack = [[CellBack alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
[self.contentView addSubview:self.cellBack];
self.cellFront = [[CellFront alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 80)];
[self.contentView addSubview:self.cellFront];
}
CellFront.m
然后调用其initWithFrame:
方法,其中规定了标签根据其种类文章内的以下方法:
- (void)setArticle:(ArticleInfo *)article {
_article = article;
self.cellFront.article = article;
}
我也是在ArticleCell.m
文件中创建的CellFront
和CellBack
UIView
小号并将它们添加到子视图中。
- (void)addControlsToView {
if ([self.article.isUserAddedText isEqualToNumber:@YES]) {
UILabel *preview = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 280, 70)];
preview.text = self.article.preview;
preview.numberOfLines = 4;
preview.font = [UIFont systemFontOfSize:16.0f];
preview.textColor = [UIColor blackColor];
preview.backgroundColor = [UIColor clearColor];
[self addSubview:preview];
}
else {
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
title.text = self.article.title;
title.font = [UIFont boldSystemFontOfSize:18.0f];
title.textColor = [UIColor blackColor];
title.backgroundColor = [UIColor clearColor];
[self addSubview:title];
UILabel *URL = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 280, 20)];
URL.text = self.article.url;
URL.font = [UIFont systemFontOfSize:16.0f];
URL.textColor = [UIColor blackColor];
URL.backgroundColor = [UIColor clearColor];
[self addSubview:URL];
UILabel *preview = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 280, 40)];
preview.text = self.article.preview;
preview.numberOfLines = 2;
preview.font = [UIFont systemFontOfSize:16.0f];
preview.textColor = [UIColor grayColor];
preview.backgroundColor = [UIColor clearColor];
[self addSubview:preview];
}
}
这就是我认为与包括有关的一切。为什么所有的单元格都显示空白?
如果您是不使用故事板或NIB来定义您的意见,awakeFromNib:
永远不会被调用。
下面是一些documentation
你应该尝试重写initWithStyle:reuseIdentifier:
代替awakeFromNib:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self != nil) {
// DO STUFF
}
return self;
}
如果你想用故事板那么你是不正确地出列的单元格,替换:
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
With:
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
如果队列中没有一个,此方法将从故事板中初始化一个新单元。你使用的方法不会。这意味着cell有时等于零,然后用initWithStyle:reuseIdentifier:
方法初始化一个新单元,由于您不是从NIB解码,所以不会调用awakeFromNib
。
以下是此方法的documentation的链接。
其他信息
你也不会看到单元格中的数据,因为你是在你使用初始化意见相同的代码块中设置标签文本值。基本上,你需要做的:
...
preview.text = self.article.preview;
...
... ...每一个细胞的再生时的addControlsToView
方法,和的UILabel的初始化只有一次的这一部分。我会将上面的代码移动到CellFront
上的文章属性的设置器中。例如,首先为标签
@property (nonatomic, strong) UILabel *preview1Label;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *URLLabel;
@property (nonatomic, strong) UILabel *preview2Label;
声明一些属性,然后像这样来初始化控制
- (void)initControls {
self.preview1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, 280, 70)];
self.preview1.text = self.article.preview;
self.preview1.numberOfLines = 4;
self.preview1.font = [UIFont systemFontOfSize:16.0f];
self.preview1.textColor = [UIColor blackColor];
self.preview1.backgroundColor = [UIColor clearColor];
[self addSubview:self.preview1];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
self.title.text = self.article.title;
self.title.font = [UIFont boldSystemFontOfSize:18.0f];
self.title.textColor = [UIColor blackColor];
self.title.backgroundColor = [UIColor clearColor];
[self addSubview:self.title];
self.URL = [[UILabel alloc] initWithFrame:CGRectMake(20, 35, 280, 20)];
self.URL.text = self.article.url;
self.URL.font = [UIFont systemFontOfSize:16.0f];
self.URL.textColor = [UIColor blackColor];
self.URL.backgroundColor = [UIColor clearColor];
[self addSubview:self.URL];
self.preview2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 280, 40)];
self.preview2.text = self.article.preview;
self.preview2.numberOfLines = 2;
self.preview2.font = [UIFont systemFontOfSize:16.0f];
self.preview2.textColor = [UIColor grayColor];
self.preview2.backgroundColor = [UIColor clearColor];
[self addSubview:self.preview2];
}
然后像这样设置的控制,有可能从文章setter方法。
- (void)setControls {
if ([self.article.isUserAddedText isEqualToNumber:@YES]) {
self.preview1.hidden = NO;
self.preview1.text = self.article.preview;
self.title.hidden = YES;
self.title.text = nil;
self.URL.hidden = YES;
self.URL.text = nil;
self.preview2.hidden = YES;
self.preview2.text = nil;
}
else {
self.preview1.hidden = YES;
self.preview1.text = nil;
self.title.hidden = NO;
self.title.text = self.article.title;
self.URL.hidden = NO;
self.URL.text = self.article.url;
self.preview2.hidden = NO;
self.preview2.text = self.article.preview;
}
}
它似乎被调用:http://i.imgur.com/c612iyG.png – 2013-04-25 23:56:54
你有一个故事板或一个NIB以及您发布的代码? – 2013-04-26 05:34:12
是的,请参阅第二段。 – 2013-04-26 15:13:54
在故事板中,您是否将原型表视图单元格的自定义类设置为“ArticleCell”? – 2013-05-01 01:05:02