我对我的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];
}
}
这就是我认为与包括有关的一切。为什么所有的单元格都显示空白?
在故事板中,您是否将原型表视图单元格的自定义类设置为“ArticleCell”? – 2013-05-01 01:05:02