我试图在退出seque之后重新加载表视图的值。过程是:我从配置文件选择视图手动执行seque,添加一个新的配置文件名,返回到配置文件选择视图。然后,我想重新加载表视图,添加新的配置文件名。它很好地运行了代码(与进入场景的原始条目相同),但我似乎无法获得numberOfRowsInSection和numberOfRowsInSection的本地方法来重新填充表视图。实际上,我必须离开屏幕,在新的配置文件名称更新之前重新输入它。有什么想法吗?
//**手工执行seque
-(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];
}
//**函数加载新的配置文件名称。
-(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;
}
}
}
//**重新加载表视图的方法
- (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;
}
发布于 2012-12-28 18:52:41
您在这里有几种不同的选择:
1)最简单的方法是每次视图控制器将要显示视图时重新加载表:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.tableView reloadData];
}
但是,它的缺点是,每次显示视图时,都会执行,即使您不一定需要重新加载数据。
2)如果您使用故事板并以iOS 6+为目标,那么当从add视图控制器返回时,您可以使用展开segue来调用视图控制器上的特定方法。有关更多信息,请参见以下问题/答案:Does anyone know what the new Exit icon is used for when editing storyboards using Xcode 4.5?
3)如果您针对的是较早版本的iOS,或者不使用情节提要,那么您可以创建一个方法的协议,只要添加了新的概要文件,就应该调用该方法,并且可以在调用该方法时重新加载该数据。这里有很多问题,这些问题涉及如何做到这一点(比如dismissModalViewController AND pass data back,它展示了如何传递数据,但是您可以做同样的事情来调用一个方法)。
发布于 2012-12-29 02:56:24
这是依纳芬格在上述评论中的实际回答。再次感谢。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
我花了一点时间才弄明白这一点,但这是因为您使用的是UIModalTransitionStylePartialCurl modalTransitionStyle。我想他们不会这么称呼它,因为它不会完全离开屏幕。如果您使用不同的样式(比如默认样式),那么它将调用viewWillAppear。如果您想保持这种状态,那么您可能应该使用其他方法之一。-伊纳菲格
https://stackoverflow.com/questions/14030901
复制相似问题