这段代码真的需要一个“[self.tableView reloadData]”吗?在 viewDidLoad 的末尾(在 UITableViewController 中)
SimpleEKDemo 示例在 RootViewController.m 文件中的 viewDidLoad 末尾有一个“[self.tableView reloadData]”。
有这个必要吗?有什么想法为什么要添加这条线吗?在 viewDidLoad 之后,视图不会通过调用诸如“cellForRowAtIndexPath”之类的方法的委托来绘制吗?
- (void)viewDidLoad {
self.title = @"Events List";
// Initialize an event store object with the init method. Initilize the array for events.
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
// Create an Add button
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAdd target:self action:@selector(addEvent:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
self.navigationController.delegate = self;
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[self.tableView reloadData]; // ** REALLY NEEDED **
}
编辑 - 我确实在 doco 中注意到了这一点(见下文) - 我仍然不清楚上面的代码行是如何需要的 - 如果你转到另一个视图,然后回到这个视图,不是这样的吗?那么如果(a)视图被重新初始化,那么无论如何它都必须再次填充自身,或者(b)如果视图不想重新初始化,则不会调用“viewDidLoad”方法,因此您不会在任何一种情况下都将“reloadData”代码行放在 viewDidLoad 方法的末尾,不是吗?
UITableView 覆盖 UIView的layoutSubviews方法所以 仅当您调用 reloadData 时 创建 UITableView 的新实例 或者当您分配新的数据源时。 重新加载表视图会清除 当前状态,包括当前 选择。但是,如果您明确 调用reloadData,它清除这个状态 以及任何后续的直接或间接 调用layoutSubviews 不会 触发重新加载。
The SimpleEKDemo sample has a "[self.tableView reloadData]" at the end of viewDidLoad in the RootViewController.m file.
Is this necessary? Any ideas why this line was put in? Wouldn't the view get drawn subsequently after the viewDidLoad via it's calls to the delegate to methods like "cellForRowAtIndexPath"?
- (void)viewDidLoad {
self.title = @"Events List";
// Initialize an event store object with the init method. Initilize the array for events.
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
// Create an Add button
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAdd target:self action:@selector(addEvent:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
self.navigationController.delegate = self;
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[self.tableView reloadData]; // ** REALLY NEEDED **
}
EDIT - I did note this in the doco (see below) - it's still not clear to me how the above line of code is required - isn't it the case that if you went to another view, and then back to this view, THEN if (a) the view is re-initialised than it would have to populate itself again anyway, or (b) if the view wan't re-initialised the "viewDidLoad" method wouldn't be called hence you wouldn't be putting the "reloadData" line of code at the end of the viewDidLoad method in either case no?
UITableView overrides the
layoutSubviews method of UIView so
that it calls reloadData only when you
create a new instance of UITableView
or when you assign a new data source.
Reloading the table view clears
current state, including the current
selection. However, if you explicitly
call reloadData, it clears this state
and any subsequent direct or indirect
call to layoutSubviews does not
trigger a reload.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有静态数据,则实际上并不需要这样做,但当您想从数据源更新表内容时,则需要这样做。
例如,
假设您要添加一条在表中输入的记录,并且希望在添加该记录时在表中显示该数据。您在第一页上有一个表,然后在第二个视图上添加一条记录,然后返回第一个视图。然后您需要显示新数据,但这次您的表的详细源方法未调用,这就是您需要重新加载数据的原因,因此此行调用表的数据源方法。
我想你可以理解现在的情况。
编辑:
当我们删除一行时,如果您不使用表调用删除,则该数据会从数据库中删除,但仍保留在表中。
现在您正在调用数据库函数并在 viewWillAppear 函数的数组中获取记录现在您可以做什么
只需从 commitEditingStyle 函数调用 viewWillappear 并在 viewWillappear 中重新加载表。在这种情况下,您需要重新加载表
This is not really needed if you have static data but when you want to update the table content from a data source then you need.
e.g.
Suppose you want add a record which you enter in a table and you want to show that data in table when you add that.you have a table on first page then add a record on second view and come back to first view.then you need to show new data but this time your deta source method for table not called thats why you need to reload the data so this line calls table's data source method.
I think you can understand the situation.
Edit:
When we delete a row then that data delete from database but remains in table if you not call delete with table.
now you are calling database function and getting record in an array at viewWillAppear function now what you can do
simply call viewWillappear from commitEditingStyle function and reload the table in viewWillappear.At this situation you need to reload the table