ios - numberOfRowsInSection 方法调用了两次

标签 ios objective-c iphone uitableview xcode6

我正在从 appcoda 开发一个简单的表格应用程序。我对 numberOfRowsInSection 方法感到困惑。当我在 numberOfRowsInSection 方法中使用 NSLog 在控制台中打印值时,控制台将值显示两次而不是一次,这意味着 numberOfRowsInSection 方法是叫了两次。

那么你能告诉我为什么会这样吗?

这是我的代码

@implementation ViewController {
    NSArray *recipes;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

}

此方法在控制台中打印两次值而不是一次

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSLog(@"No of Recipe :- %d",[recipes count]);
    return [recipes count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *tableIdentifier = @"recipeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
    }
    cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
    return cell;
}

最佳答案

这可能是一种正常行为。在 numberOfRows 方法中放置一个断点并查看调用堆栈。您可以看到框架本身从两个不同的流中调用了 numberOfRows。就我而言,我可以看到:

  1. 一旦它被 [UITableView didMoveToWindow] 调用
  2. 另一个来自[UITableView layoutSubviews]

关于ios - numberOfRowsInSection 方法调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493276/

相关文章:

iphone - 是否可以在 iOS 应用程序中显示本地化定价信息

ios - 将JSON文件保存到iPhone

ios - 如何以最高精度比较小数点后30位的双数?

ios - UIScrollView 卡住了

objective-c - 如何在 Objective C 中创建 Json 请求

iphone - 通过prepareForSegue传递信息时出现问题,我无法弄清楚

ios - 在 iOS 上渲染到 CVPixelBuffer

ios - CocoaLumberjack 自定义日志级别

ios - 每次从 TestFlight 首次启动时应用程序崩溃

iphone - 非法尝试建立关系