从另一个UIViewController刷新表

时间:2014-07-10 15:56:32

标签: ios objective-c uitableview uiviewcontroller

我有两个视图控制器,其中一个(ViewController)有一个名为tableView的表。

我想从我的其他视图控制器(pageView)刷新此表。

我在pageView

中试过这个
ViewController*refresh;
[refresh.tableView reloadData];

但这不起作用。

两个视图控制器之间的连接segue是push segue

我该怎么办?我应该通过故事板segue做到吗?

16 个答案:

答案 0 :(得分:19)

选项1

@ Class2

@property (nonatomic) BOOL shouldRefresh; // in .h file

- (void)viewWillAppear:(BOOL)animated // in .m file
{
    [super viewWillAppear:animated];
    if (_shouldRefresh) [self.tableView reloadData];
}

@ Class1的

// Add this in any method where you want it to refresh and push to view
ClassTwoController *viewController = [[ClassTwoController alloc] init];
[viewController setShouldRefresh:YES];
[self.navigationController pushViewController:viewController animated:YES];

<强> *更新:

选项2

@Class 2

// Add this line in viewDidLoad in same class you want the tableView to refresh
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableWithNotification:) name:@"RefreshTable" object:nil];

// Add this method just beneath viewDidLoad:
- (void)refreshTableWithNotification:(NSNotification *)notification
{
    [self.tableView reloadData];
}

@ Class1的

// Call this when ever you want to refresh the tableView in Class2
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:nil userInfo:nil];

答案 1 :(得分:3)

/ *在ViewController的viewDidLoad方法中添加此行(通过使用此行,您将向ViewController类添加通知观察者)* /

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableview:) name:@"ReloadTableview" object:nil];

/ *在ViewController类中添加此方法(在pageView中发布通知时此方法调用)* /

- (void)reloadTableview:(NSNotification *)notif
{
     [tableView reloadData];
}

/ *在您要刷新的时间/地点添加此行(在ViewController中添加此行发布通知)* /

[[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTableview" object:nil];

答案 2 :(得分:2)

在ViewController1中:

> Create property for UITableview with nonatomic and retain
> Now Create table Object.

在ViewController2.m中:

ViewController1 *objV1 = [ViewController1 alloc]initWithNibName....];   

[objV1.yourTableView reloadData];

答案 3 :(得分:2)

如果已加载第二个视图控制器,则可以在firstviewcontroller中使用NSNotification。此通知将在第二个视图控制器中调用方法。在此方法中编写代码以重新加载tableview。

答案 4 :(得分:1)

如果我理解正确,您需要重新加载存储在导航堆栈的上一个视图控制器中的表视图。如果是这样,您可以通过导航控制器的viewControllers属性访问它。

NSUInteger indexOfTableController = self.navigationController.viewControllers.count - 2;
ViewController *tableController = (ViewController*)[self.navigationController.viewControllers objectAtIndex:indexOfTableController];
[tableController.tableView reloadData];

答案 5 :(得分:1)

大多数情况下会发生这种情况,因为您没有正确初始化对第二个控制器的引用,并且它保持为零。必须是这样的。

@implementation
{
    ViewController *refresh;
}

- (void) openNewController
{
    refresh = [[ViewController alloc] init];
    [self.navigationController pushViewController:refresh animated:true];
}

- (void) refreshNewController 
{
    [refresh.tableView reloadData];
}

@end

答案 6 :(得分:1)

好的,为了举例,我们可以调用前面的ViewController VC1和当前的ViewController VC2。

所以你从VC1到VC2进行推送。你可以做的是:在segue期间将TableView的引用传递给VC2。所以你要在VC2中创建一个公共UITableView tblView变量,然后在VC1中实现prepareForSegue方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Segue_Identifier_From_StoryBoard"])
    {
        // get your destination view controller
        VC2 *vc2 = [segue destinationViewController];

        vc2.tblView = TableView;
    }
}

然后在VC2中,您可以调用[tblView reloadData];

完成VC2后,确保你没有使用tblView引用。

答案 7 :(得分:1)

你可以检查viewcontroller.tableview和refresh.table View的地址,我认为它们会不一样; 你可以为viewcontroller制作单例,然后调用例如

viewcontroller *vc = [viewcontroller sharedInstance];
[[vc tableview] reloadData];

答案 8 :(得分:1)

在你的pageView中创建一个方法

-(void) reloadVCController{

  ViewController*vc=[[ViewController alloc]init]];
    [vc.tableview reloadData;
}

并将其用于您想要的地方

[self reloadVCController];

答案 9 :(得分:1)

ViewController*refresh;
[refresh.tableView reloadData];

通过查看上述代码,您未将ViewController instance分配给refresh。执行reloadData()时,您的refresh将为零,因此显然不会对任何操作做出反应。

ViewController*refresh = originalInstance; //assign your original instance here
    [refresh.tableView reloadData];

这会起作用,这里要注意的要点是你的刷新viewcontroller应该是堆栈中最顶层的实例,因为任何UI操作都应该在主线程中发生。

答案 10 :(得分:1)

使用NSnotification center在该视图控制器中添加观察者并为重载表编写方法。 [self.tableView reloadData]; 并从当前视图发布通知观察者。

答案 11 :(得分:1)

如果您已经尝试了所有答案,确保在重新加载tableView之前,dataSource实际上已更新,带有新/附加/内容。

答案 12 :(得分:1)

Reloaddata需要在parentviewcontroller中执行,因为它调用tableview委托方法来重新加载表,例如CellForRowAtIndexpath等等。  这意味着您需要在parentviewcontroller中定义一个包含reloaddata命令的公共方法,并从子视图控制器中调用它。

公共方法:

Parentviewcontroller.h

@interface 

 -(void) reloadTable:(id) sender;

parentviewcontroller.m

@implementation

- (void) reloadTable:(id) sender {
   [self.tableview reloaddata];
   return;
 }

childviewcontroller.h

 #import parentviewcontroller.h
  @class parentviewController 
  @interface childviewcontroller :UIViewController (or UITableViewController)

  @property (nonatomic, strong) parentViewController *parent;

childviewController.m

@implementation  
 @synthesize parent; 

 /*  Note the parent instance is passed from the parent to the child view controller,      
 it is not allocated and initialised in the child controller -  many ways to do this 
 depending on coding style  */

/* after updating the data */   

 [parent reloadTable];

答案 13 :(得分:1)

我认为问题是你没有加载刷新控制器视图。因此, reloadTable 消息只会变为零。您可以通过调用 view getter

强制View Controller加载其视图
[viewController view];

因此,请检查 tableView 是否为零,如果是,请使用所描述的方法对其进行初始化。

答案 14 :(得分:1)

作为情感的化妆答案:

如果您想跳过启动动画,只需在ClassTwoController中设置一个bool属性,以检查它是否是第一次加载此视图

ClassTwoController.h:

@property (nonatomic, assign) BOOL notFirstTimeRun;

ClassTwoController.m:

 -(void)viewWillAppear:(BOOL)animated // in .m file
{
    if(self.notFirstTimeRun) {
        [self.tableView reloadData];
    } else {
         [self playStartUpAnime];
    }
}

 -(void)playStartUpAnimation // in .m file
{
    //Place your codes for animation here
}

ClassOneController。m中使用check bool初始化它:

ClassTwoController *viewController = [[ClassTwoController alloc] init];
viewController.notFirstTimeRun = YES;

答案 15 :(得分:1)

如果您有父子关系,您可以将弱属性添加到子视图控制器,如此

@property (nonatomic, weak) ParentViewController *parentController;

然后,当您需要重新加载表格时,只需拨打[parentController.tableView reloadData]

即可

其他方式是使用NSNotificationCenter。在控制器中,您想要重新加载表,您应该订阅通知([[NSNotificationCenter defaultCenter] addObserver...])并实现您为此方法提供的选择器。在另一个控制器中,您应该发布具有该名称的通知。

如果符合您的需要,您也可以刷新viewWillAppear。