[英]Why are my cells appearing blank in my UITableView?
我的UITableView
有一些奇怪的結構。 基本上,我有構成表的ArticleCell
對象( UITableViewCell
子類),每個ArticleCell
由“前”視圖和“后”視圖組成。 前視圖包含所有標簽和用戶看到的內容,而背面有左右兩個圖標。 這兩個視圖的想法是,用戶可以向右或向左滑動頂視圖以快速選擇一個選項(類似於Reeder)。
我在故事板中稍微實現了這一點,但主要是在代碼中。 我在故事板中唯一做的就是完成了UITableViewController
的布局,並命名了原型單元的標識符(標識符:“ArticleCell”)。
除了故事板,正如我所說,一切都是在代碼中完成的。 我從Core Data獲取單元格的信息,並通過將文章設置為單元格的article
屬性來構造單元格,然后將該文章設置為CellFront
UIView
的article
屬性。 因為有兩種單元CellFront
,具體取決於單元格所包含的文章類型,所以在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;
}
我還創建了CellFront
和CellBack
UIView
在S ArticleCell.m
文件:
- (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或NIB來定義視圖,那么awakeFromNib:
將永遠不會被調用。
這是一些文檔
您應該嘗試重寫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];
}
附:
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
如果隊列中沒有新單元,則此方法將從故事板初始化新單元。 你使用的方法不會。 這意味着有時單元格將等於nil,然后使用initWithStyle:reuseIdentifier:
方法初始化一個新單元格,該方法不會調用awakeFromNib
因為您沒有從NIB解碼它。
以下是此方法文檔的鏈接。
附加信息
您也不會在單元格中看到數據,因為您要在用於初始化視圖的同一代碼塊中設置標簽文本值。 基本上,你需要做:
...
preview.text = self.article.preview;
...
...每次重復使用單元格時, addControlsToView
方法的這一部分,以及UILabel的初始化僅一次。 我將上面的代碼移動到CellFront
上的文章屬性的setter中。 例如,首先聲明標簽的一些屬性
@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];
}
然后這樣設置控件,可能來自article屬性的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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.