iOS 7自定义单元格未在表格视图中显示

c11*_*ada 4 objective-c storyboard ios

请耐心等待,我刚刚开始iOS开发.我试图tableViewCell在故事板中显示自定义tableView.我做了以下事情.

.xibtableViewCell它创建了一个新的

在此输入图像描述

然后我为此创建了一个自定义类..h文件看起来像这样

#import <UIKit/UIKit.h>

@interface CustomTableCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *thumbnailImageView;
@property (weak, nonatomic) IBOutlet UILabel› *titleLabel;

@end
Run Code Online (Sandbox Code Playgroud)

然后在我的TableViewController.m我导入了CustomTableCell.h,我正在做10行的跟随

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomTableCell";
    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.titleLabel.text ="test text";


    return cell;
}
Run Code Online (Sandbox Code Playgroud)

这似乎很好,但是当项目加载时没有任何反应.任何建议都会很棒.

我在cellForRowAtIndexPath方法中放置了断点,但它从未达到过这一点.这是模拟器的屏幕截图

在此输入图像描述

Ste*_*eve 13

您必须注册.xib文件.在您的viewDidLoad方法中,添加以下内容:

[self.tableView registerNib:[UINib nibWithNibName:@"CustomTableCell" bundle:nil] 
     forCellReuseIdentifier:@"CustomTableCell"];
Run Code Online (Sandbox Code Playgroud)


mat*_*att 9

你加载笔尖的方式真的过时了,过时了.注册笔尖并使用它会好得多(自iOS 6起)dequeueReusableCellWithIdentifier:forIndexPath:.

请参阅我对获取自定义单元格的所有四种方法的解释.


And*_*sek 7

确保在UITableView的故事板中设置了委托和数据源.这将确保cellForRowAtIndexPath为每一行调用.您可以NSLog在该方法中放置一条消息来验证相同的内容.

此外,由于您使用的是故事板,因此您可能需要查看UITableView的Prototype Cells.它们是一种更容易做同样事情的方法 - 使用自定义单元格创建UITableView.

这是在故事板的UITableView中使用Prototype单元的一个不错的教程:

http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1


Mel*_*ram 5

- (void) viewDidLoad {

    [super viewDidLoad];

    UINib *cellNib = [UINib nibWithNibName:@"CustomTableCell" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:@"CustomTableCell"];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomTableCell";
    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.titleLabel.text ="test text";


    return cell;
}
Run Code Online (Sandbox Code Playgroud)