我已经设置了一个简单的UITableViewController
没有内容的演示应用程序,但工具栏中有一个“添加”按钮。这将启动一个模态视图控制器,除了“取消”按钮之外,它再次为空。取消按钮只是告诉其delegate
(UITableViewController
)取消模态。
然后我在NSLog
的{{1}}方法中添加了UITableViewController
语句。
通常,当表视图控制器加载时,我看到两次调用numberOfSectionsInTableView
。当我打开并关闭模态(返回numberOfSectionsInTableView
)时,我看不到对UITableViewController
的进一步调用。
但是,如果我从numberOfSectionsInTableView
返回0
,除了显示的两个调用之外,我还会在模式被解除时看到额外的numberOfSections调用。
仅当numberOfSectionsInTableView
返回numberOfSectionsInTableView
时才会发生这种情况,除了上面提到的内容之外,我没有为我的项目添加其他代码。通过设置我所描述的几个控制器并修改0
的结果,可以轻松验证这一点。
我的问题:
numberOfSectionsInTableView
调用UITableView
从模态视图返回?numberOfSectionsInTableView
返回numberOfSectionsInTableView
时才会这样做?0
之外,numberOfSectionsInTableView
在模式被解除时也会调用UITableViewController
。实际上,它正在尝试显示其cellForRowAtIndex:
的新内容。如果添加的第一行已经自动更新,我的意思是如何手动设置行插入动画?我不应该确保我的dataSource
与其UITableView
一致吗?dataSource
检查知道有一个或多个部分(因此询问我的代表有多少部分)是什么属性?它不能是UITableViewController
本身,因为每当我从模态返回时我都会看到它,而不仅仅是当numberOfSections = 0时。答案 0 :(得分:2)
当表格视图即将在第一次加载时出现时, table-view controller重新加载表视图的数据......
UITableViewController
类在超类方法中实现它viewWillAppear:
如果您在调试器中观看,应用启动时的第二次调用来自UITableViewController
viewWillAppear:
实施 - 特别是上面提到的部分,其中tableView
会发送reloadData
消息。
现在,启动时对numberOfSectionsInTableView:
的第一次调用也来自UITableViewController
viewWillAppear:
的实施,但不是直接来自该实施&# 39;呼叫-[UITableView reloadData]
。我不确定第一个电话是什么。
但是,对于您的问题,在解除模态时发生的对numberOfSectionsInTableView:
的调用与来自applicationDidFinishLaunching:withOptions:
的第二次调用具有完全相同的调用堆栈。我的假设是UITableView
解释零部分处于完全没有加载的状态。实际上这确实有道理。我考虑过一个空的"表视图是一个没有任何行的视图,但没有任何部分的视图几乎"未初始化"对我来说。此外,UITableViewDataSource
文档暗示UITableView
默认有一个部分。从这种方法返回零也与文档的假设不一致。
现在,您对动画的关注 - 如果您给表格一个空白部分,您将可以完全控制插入第一行的任何动画,而不是锁定当你需要重新加载时。
我认为这个故事的寓意是,除非你真的,真的需要出于某种原因,否则不会返回零部分。你的帖子的标题是指这个表格视图是"空的"同样,但我认为很清楚,框架发现零部分不是空的,而是卸载的。
希望这有帮助!感谢您发布示例项目供我使用。
答案 1 :(得分:1)
也许代表无法相信它的眼睛。但严重的是,由于表视图必须至少有一个部分,因此传递0没有任何意义。为什么这样?你传递一个无效的参数,它会给你一个奇怪的回应。至于为什么当你传递1时它没有要求多个部分,我认为这是因为它不需要知道(从模态视图控制器返回) - 表视图已经填充(如果有一些数据)并且您没有更改模型中的任何内容,那么它不需要更新。
我在你的示例项目中添加了几行,每次从模态视图控制器返回时都会连续滑动,这是我认为你要做的。我为numberOfRowsInSection的返回值添加了一个int属性num,添加了一个填充表的数组,以及从模态视图控制器解雇方法调用insertRowsAtIndexPaths。
- (void)viewDidLoad
{
_num = 0;
self.theData = @[@"one",@"two",@"three"];
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addRecipe)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"# sections requested");
//when 0, this fires on return from the modal. When 1, it does not.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"in numberOfRows in section");
return _num;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"in cellForRowAtIndexPath");
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.text = [self.theData objectAtIndex:indexPath.row];
return cell;
}
- (void)addRecipe
{
//create the modal and suscribe for delegate notifications
AddRecipeViewController *addRecipeController = [[AddRecipeViewController alloc]initWithStyle:UITableViewStyleGrouped];
addRecipeController.delegate = self;
//display the modal in a navigation controller
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addRecipeController];
[self.navigationController presentModalViewController:navController animated:YES];
}
- (void)addRecipeVC:(AddRecipeViewController *)addRecipeVC didAddRecipe:(NSString *)recipe
{
[self dismissModalViewControllerAnimated:YES];
_num += 1;
[self performSelector:@selector(addRow) withObject:nil afterDelay:.5];
}
-(void)addRow {
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_num-1 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
}
答案 2 :(得分:0)
tableview在用数据填充表格视图时检查部分的数量!
由于表格可以分为几个部分,因此必须具体知道将其分成多少部分。
重新加载数据时,还会检查节数。
因为每次表视图必须采取操作来访问表的数据,例如您点击的行,在哪个部分,或填充数据表,必须知道部分的数量!
希望这有帮助!