tableview委托方法从未调用过

时间:2011-10-17 03:30:18

标签: 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;
}

我为每个方法设置了一个断点,我可以找到

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

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

从未调用,但可以调用其他两种方法。所以有什么问题?谢谢。

更新

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

6 个答案:

答案 0 :(得分:12)

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

在代码中使用这两行

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

答案 1 :(得分:5)

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

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

答案 2 :(得分:3)

确保已通过代码或通过Interface Builder插座将表视图连接到其委托。

答案 3 :(得分:2)

我遇到过类似的问题。数据源正在运行,但委托并非如此。如果您添加了" UITapGestureRecognizer" to" self.view",这就是在这里造成问题。在这种情况下,将不会调用与用户交互相关的表委托。因为"等代表确实选择了#34;任何用户交互发生时调用。在这种情况下,如果在视图上有UITapGestureRecognizer,则表委托将不起作用。

答案 4 :(得分:0)

您是否已将tableView的委托设置为您的类?

此外,除非你是UITableViewController的子类,否则你也应该实现

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

答案 5 :(得分:0)

如果你已经设置了所有代理,那么问题可能是UITableview的大小小于单元格的大小。