【发布时间】:2013-04-25 22:03:14
【问题描述】:
我的UITableView 的结构有点奇怪。基本上,我有构成表格的ArticleCell 对象(UITableViewCell 的子类),每个ArticleCell 都由一个“前”视图和一个“后”视图组成。前视图包含用户看到的所有标签和其他内容,而后视图具有左右两个图标。这两个视图的想法是,用户可以向右或向左滑动顶视图以快速选择一个选项(有点像 Reeder 中的)。
我在情节提要中稍微实现了这一点,但主要是在代码中。我在情节提要中所做的唯一一件事就是完成了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中,我覆盖了文章的set方法,所以当上面的数据源方法调用它时,它也可以设置视图的article属性。
- (void)setArticle:(ArticleInfo *)article {
_article = article;
self.cellFront.article = article;
}
我还在ArticleCell.m 文件中创建了CellFront 和CellBack UIViews:
- (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)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];
}
}
这就是我认为相关的所有内容。为什么所有单元格都显示为空白?
【问题讨论】:
-
在storyboard中,是否将原型表视图单元格的Custom Class设置为
ArticleCell?
标签: ios objective-c uitableview nsfetchedresultscontroller