[英]custom cell not appear in tableview
我使用情節提要。 我在“重用標識符”“ userFullCell”中編寫的情節提要中為單元格創建新類。 我的細胞班.h
#import <UIKit/UIKit.h>
@interface UsefullLinksCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *shortTitle;
@end
.m默認
#import "UsefullLinksCell.h"
@implementation UsefullLinksCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
屬性圖像和shortTitle在故事板中與Cell中的UILabel和UIImageView連接
然后在帶有UITableView的類中,導入“ UseFullLinksCell.h”,並在viewDidLoad中寫道:
[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:@"userFullCell"];
tableView是我的出路:
@property (weak, nonatomic) IBOutlet UITableView *tableView;
然后我嘗試創建單元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"userFullCell";
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *info = resourceArray[indexPath.row];
if(info){
cell.image.image = info[@"logo"];
cell.shortTitle.text = info[@"title"];
}
return cell;
}
因此,單元格初始化,但未分配圖像和文本,並且在方法結束后為單元格返回0x0000000。 如果我用這種方法為標准單元格代碼創建,我的意思是UITableViewCell * cell,一切都很好。 我可能在哪里犯錯? PS對不起,我無法從情節提要中提供屏幕截圖,因為我不是從計算機上書寫的,如果需要,我稍后會提供圖像
您應該在創建自定義單元而非類的位置注冊xib文件(使用registerNib:forCellReuseIdentifier :)。 另外,我不知道這是否會有所作為,但請替換為:
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
這樣(注意方法末尾的forIndexPath:):
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
編輯后:
如果將一個自定義單元格添加到情節提要中的表視圖中,並為其賦予標識符“ userFullCell”,則您無需注冊任何內容-實際上,注冊您的類會使該類不起作用。 因此,只需注釋掉該register語句,然后看看是否可行。
您錯過了一些東西:
NSArray *objects=[[NSBundle mainBundle]loadNibNamed:@"UsefullLinksCell" owner:self options:nil];
UsefullLinksCell *cell =[objects objectAtIndex:0];
這將為您提供自定義單元格的對象。
我認為您必須執行以下操作
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
image=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50.0,50.0)];
shortTitle=[[UILabel alloc] initWithFrame:CGRectMake(60.0,0.0,200.0,50.0)];
}
return self;
}
否則所有這些都應該工作。問題是您的組件沒有在任何地方初始化,應該從xib加載它,或者必須手動對其進行初始化。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.