tableview委托方法从未调用过

Gao*_*188 0 iphone objective-c uitableview

我有一个viewcontroller,并实现委托UITableViewDelegate和UITableViewDataSource,所以我有以下方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [_nameArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"name";
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
   static NSString *TableIdentifier = @"TableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier]autorelease];
    }
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textAlignment = UITextAlignmentLeft;    
    nameLabel.text = @"100";
    nameLabel.backgroundColor = [UIColor blackColor];
    nameLabel.textColor = [UIColor yellowColor];
    [cell.contentView addSubview:nameLabel];
    [nameLabel release];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我可以找到每个方法的断点

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

从未调用,但可以调用另外两种方法.所以有什么问题?谢谢.

更新:

我已经在IB中将datasoucre和委托连接到文件的所有者,但无法解决问题.

nik*_*nik 12

很可能是因为委托引用问题..确保你已经给出了委托方法的正确引用.

在代码中使用这两行

self.tableView.delegate = self;
self.tableView.dataSource = self;
Run Code Online (Sandbox Code Playgroud)


UPT*_*UPT 5

_nameArray有0个对象,这就是为什么它不会出现cellForRowAtIndexPath,因为如果_nameArray是nil或0对象,则委托方法numberOfRowsInSection返回0并且它不会出现在上述两种方法中.

为了快速检查,您可以从方法numberOfRowsInSection返回带符号的值10,然后看,它将调用cellForRowAtIndexPath方法.