xcode,在退出序列时重新加载表视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14030901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode, reloading table view on exiting seque
提问by GrymHyman
I'm trying to reload the values of a table view after exiting from a seque. The process being: I perform the seque manually from the profile selection view, add a new profile name, return to the profile selection view. Then I would like to reload the table view adding the new profile name. It is running the code fine (same code as original entry into the scene), but I can't seem to get the native methods of numberOfRowsInSectionand numberOfRowsInSectionto repopulate the table view. I actually have to leave the screen and reenter it before the new profile name will update. Any thoughts?
从序列退出后,我试图重新加载表视图的值。过程是:我从配置文件选择视图手动执行序列,添加新的配置文件名称,返回到配置文件选择视图。然后我想重新加载添加新配置文件名称的表视图。它运行代码很好(与原始进入场景的代码相同),但我似乎无法获得numberOfRowsInSection和numberOfRowsInSection的本机方法来重新填充表视图。在新的配置文件名称更新之前,我实际上必须离开屏幕并重新输入它。有什么想法吗?
//** performing seque manually
//** 手动执行序列
-(IBAction)buttonAddNewProfile:(id)sender
{
// creating object for profile selection screen
UIStoryboard *ProfileSelectionStoryboard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
// creating object for add new profile storyboard
AddNewProfileViewController *addnewprofileVC=[ProfileSelectionStoryboard instantiateViewControllerWithIdentifier:@"Add New Profile"];
// setting the transition style
addnewprofileVC.modalTransitionStyle=UIModalTransitionStylePartialCurl;
// performing the segue
[self presentViewController:addnewprofileVC animated:YES completion:nil];
// performing new table view load on return from new profile
[self loadUsers];
}
//** function to load the new profile names in.
//** 加载新配置文件名称的函数。
-(void)loadUsers
{
// retreiving the users from the database
SQLiteFunctions *sql = [[SQLiteFunctions alloc] init];
// testing for successful open
if([sql openDatabase:@"users"])
{
// setting query statement
const char *query = "SELECT * FROM users;";
// testing for that profile name existing already
if([sql getUserRecords:query] > 0)
{
// initializing array
NSMutableArray *names = [[NSMutableArray alloc] init];
// loop through object compling an array of user names
for(Users *ProfileUser in sql.returnData)
{
// adding user name to the listview array
[names addObject:ProfileUser.user_name];
}
// setting table view array to local array
tableData = names;
}
}
}
//** methods to reload the table view
//** 重新加载表格视图的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
// returning the number of rows in the table
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// setting up the table view cells for data population
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
// testing for cell parameters
if (cell == nil)
{
// setting up cloned cell parameters
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
}
// setting cell values to the array row value
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
// returning the current row label value
return cell;
}
回答by lnafziger
You have a few different options here:
您在这里有几个不同的选择:
1) The easiest is to simply reload the table every time that the view controller is about to display its view:
1) 最简单的方法是在每次视图控制器即将显示其视图时简单地重新加载表:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
The downside though, is that this will be executed everytime that the view is displayed, even when you don't necessarily need to reload the data.
但缺点是每次显示视图时都会执行此操作,即使您不一定需要重新加载数据。
2) If you are using storyboard's and targeting iOS 6+ then you can use an unwind segue to call a specific method on your view controller when going back from the add profile view controller. For more info, see this SO question/answers: Does anyone know what the new Exit icon is used for when editing storyboards using Xcode 4.5?
2) 如果您使用的是情节提要并针对 iOS 6+,那么当从添加配置文件视图控制器返回时,您可以使用 unwind segue 来调用视图控制器上的特定方法。有关更多信息,请参阅此 SO 问题/答案:有谁知道使用 Xcode 4.5 编辑故事板时新的退出图标的用途是什么?
3) If you are targeting older versions of iOS or aren't using storyboards, then you can create a protocol with a method that should be called whenever a new profile is added and you can reload the data whenever that method is called. There are lots of questions here on SO which cover how to do this (like dismissModalViewController AND pass data backwhich shows how to pass data, but you can do the same thing to just call a method).
3) 如果您的目标是旧版本的 iOS 或不使用故事板,那么您可以创建一个协议,该协议的方法应该在添加新配置文件时调用,并且您可以在调用该方法时重新加载数据。这里有很多关于 SO 的问题,它们涵盖了如何执行此操作(例如dismissModalViewController 并传回显示如何传递数据的数据,但您可以执行相同的操作,只需调用一个方法)。
回答by GrymHyman
This the actual answer from lnafzinger in the comments above. Thanks again.
这是 lnafzinger 在上述评论中的实际回答。再次感谢。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
It took me a bit to figure this out, but it is because you are using the UIModalTransitionStylePartialCurl modalTransitionStyle. I guess they don't call it because it doesn't completely leave the screen. If you use a different style (like the default) then it will call viewWillAppear. If you want to stay with that, then you should probably use one of the other methods. – lnafziger
我花了一点时间才弄清楚这一点,但这是因为您使用的是 UIModalTransitionStylePartialCurl modalTransitionStyle。我猜他们不会调用它,因为它没有完全离开屏幕。如果您使用不同的样式(如默认样式),它将调用 viewWillAppear。如果您想坚持下去,那么您可能应该使用其他方法之一。– lnafziger