关闭 UIViewController 后如何重新加载 tableView
Posted
技术标签:
【中文标题】关闭 UIViewController 后如何重新加载 tableView【英文标题】:How to reload tableView after dismissing a UIViewController 【发布时间】:2012-04-07 16:10:47 【问题描述】:我使用下面的方法来呈现一个名为 DictAddSubSecondCell 的 UIViewController:
// UIViewControler 1
- (IBAction)addWord:(id)sender
dictAddSubSecondCellController = [[DictAddSubSecondCell alloc] initWithNibName:@"DictAddSubSecondCell" bundle:nil];
[self presentModalViewController:dictAddSubSecondCellController animated:YES];
[dictAddSubSecondCellController release];
当我点击 DictAddSubSecondCell 中的按钮时:
- (IBAction)dismissAction:(id)sender
[self dismissModalViewControllerAnimated:YES];
UIViewControler 1 的 tableView 没有重新加载它的数据,但是我在 viewWillApear 方法中添加了一个方法:
[self.table reloadData];
但它仍然无法正常工作。我对此一无所知。对我有什么建议吗?谢谢。
这里是 UIViewcontroller 1 的完整源代码:
//
// DictAddSubSecond.m
//
// Created by Samuel Armstrong on 4/8/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "DictAddSubSecond.h"
#import "DictionaryList.h"
@implementation DictAddSubSecond
@synthesize dictList, table, editButton;
@synthesize dictAddSubSecondCellController;
- (IBAction)addWord:(id)sender
dictAddSubSecondCellController = [[DictAddSubSecondCell alloc] initWithNibName:@"DictAddSubSecondCell" bundle:nil];
[self presentModalViewController:dictAddSubSecondCellController animated:YES];
[dictAddSubSecondCellController release];
- (IBAction)toggleEdit
[self.table setEditing:!self.table.editing animated:YES];
if (self.table.editing)
[editButton setTitle:@"Done"];
[editButton setStyle:UIBarButtonItemStyleDone];
else
[editButton setTitle:@"Edit"];
[editButton setStyle:UIBarButtonItemStyleBordered];
- (IBAction)dismissAction:(id)sender
[self dismissModalViewControllerAnimated:NO];
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
#pragma mark - View lifecycle
- (void)refreshTableData
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [path stringByAppendingPathComponent:@"dictWithWords.tmp"];
if (dictList == nil)
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:file];
if ([array objectAtIndex:0] != nil)
dictList = [[NSMutableArray alloc] initWithCapacity:1];
self.dictList = array;
[array release];
else
dictList = [[NSMutableArray alloc] initWithCapacity:1];
[self.table reloadData];
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
[self refreshTableData];
- (void)someMethodToReloadTable:(NSNotification *)notification
[table reloadData];
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethodToReloadTable) name:@"reloadTable" object:nil];
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"reloadTable" object:nil];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [dictList count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *MoveMeCellIdentifier = @"MoveMeCellIdentifier";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:MoveMeCellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MoveMeCellIdentifier] autorelease];
//A Boolean value that determines whether the cell shows the reordering control.
cell.showsReorderControl = YES;
NSUInteger row = [indexPath row];
cell.textLabel.text = [dictList objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
#pragma mark -
#pragma mark Table View Delegate Methods
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
NSUInteger row = [indexPath row];
[self.dictList removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationMiddle];
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
return UITableViewCellEditingStyleDelete;
// Asks the data source whether a given row can be moved to another location in the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
return YES;
//Tells the data source to move a row at a specific location in the table view to another location.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
NSUInteger fromRow = [fromIndexPath row];
NSUInteger toRow = [toIndexPath row];
id object = [[dictList objectAtIndex:fromRow] retain];
[dictList removeObjectAtIndex:fromRow];
[dictList insertObject:object atIndex:toRow];
[object release];
@end
【问题讨论】:
逻辑似乎是合理的。您是否可以使用日志消息或调试器来检查两件事:[self.table reloadData]
是否真的在那里被调用,如果是,您是否看到此后对您的委托方法的任何调用(例如numberOfRowsInSection:
)?
我在 DictAddSubSecondCell 的 Documents/ 文件夹中修改了一个文件(使用 writeTOfile 方法的 NSArray 实例),我想在 UIViewControler 1 中显示修改后的结果。我只能通过重新加载 UIViewControler 1 才能看到新结果现在。
这意味着 TableView 在 modalview 关闭后没有重新加载
【参考方案1】:
尝试在viewDidAppear:
中调用它而不是viewWillAppear:
。
【讨论】:
你确定 viewDidAppear: 被调用了吗?将 NSLog 语句放入其中以确保。委托方法很容易打错,调用时必须准确。 - (void)viewWillAppear:(BOOL)animated 在我关闭时被调用。 那么可能新数据没有正确添加到您的模型中。返回第一个视图控制器时使用日志语句检查数据。 问题解决了!虽然我将委托和数据源链接到它的文件所有者,但它看起来不起作用,所以我添加了 self.table.delegate = self; && self.table.datasource = self;在 viewDidLoad 方法中,它会正常工作。!以上是关于关闭 UIViewController 后如何重新加载 tableView的主要内容,如果未能解决你的问题,请参考以下文章
当模态视图(第二个视图)被关闭时刷新 ViewController 中的核心数据 - Swift
如何在 phonegap 项目的闪屏后添加 UIViewController?