Prototype Cell在ios 7+上有空白内容

时间:2015-03-23 19:33:28

标签: ios objective-c uitableview

这个让我发疯 - 我不知道自己错过了什么。

这是我的ViewController。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    [self.tableView registerClass:[CurrentMatchCell class] forCellReuseIdentifier:@"CurrentMatchCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

}

#pragma mark -
#pragma mark Table View Data Source Methods

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"2");
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"3");

    CurrentMatchCell *cell = (CurrentMatchCell *)[tableView dequeueReusableCellWithIdentifier:@"CurrentMatchCell"];

    if (cell == nil) {
        NSLog(@"XXX");
    }
    [cell.matchDescription setText: @"Home Team vs Away Team"];

    return cell;
}

以下是该应用的截图。

View Controller in scoreboard

委托和数据源以编程方式设置。

enter image description here

单元格属性:

enter image description here enter image description here

和.h文件:

@interface CurrentMatchesViewController : UIViewController <NSFetchedResultsControllerDelegate,UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

所以,我可以看到日志1,2,3被打印出来,单元格不是nill但我看不到我的内容。那是为什么?

我只看到一些空的白色单元格(即使我返回0或每次都显示相同的内容)。

由于

2 个答案:

答案 0 :(得分:1)

如果您在故事板中创建表格视图和单元格原型,则storyboard加载程序会负责注册您在故事板中定义的单元格原型。所以:

  1. 您无需再次在代码中调用registerClass:forCellReuseIdentifier:。这实际上会搞乱你的故事板设置。

  2. 您也可以使用dequeueReusableCellWithIdentifier:forIndexPath:代替dequeueReusableCellWithIdentifier:。该方法始终返回一个单元格,因此您无需进行nil检查。

  3. 编辑:如果这不起作用,请在设置委托/数据源后尝试调用[self.tableView reloadData],或在故事板中设置委托和数据源。

答案 1 :(得分:0)

这是因为您没有加载自定义笔尖。试试这个。 (确保CurrentMatchCell是xib文件的名称)。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    UINib *nib = [UINib nibWithNibName:@"CurrentMatchCell" bundle:nil];
   [self.tableView registerNib:nib forCellReuseIdentifier:@"CurrentMatchCell"];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

}

修改:根据您的评论:在使用故事板时不要注册自定义单元格类....它会为您执行此操作并且还会设置委托。所以尝试从viewdidload中删除那些行....然后我会尝试将CurrentMatchesViewController作为UITableViewController的子类