gpt4 book ai didi

ios - numberOfRowsInSection 方法调用了两次

转载 作者:行者123 更新时间:2023-11-28 17:52:32 25 4
gpt4 key购买 nike

我正在从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com