从另一个视图重新加载表视图

Posted

技术标签:

【中文标题】从另一个视图重新加载表视图【英文标题】:Reloading table view from another view 【发布时间】:2013-12-10 04:24:45 【问题描述】:

在我的 ios 应用程序中,我需要从另一个视图更新 tableview。我尝试使用 NSNotifications 但没有成功。第一个视图包含一个使用 fetch 请求从核心数据填充的 tableview。这个视图还有一个添加按钮,它打开一个视图以将对象添加到核心数据实体。第二个视图有几个文本字段、一个保存按钮和一个返回按钮,它再次打开第一个视图,但行没有更新。 这是第一个视图控制器的代码:

#import "RootViewController.h"
#import "AddToDoViewController.h"


@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end


@implementation RootViewController

@synthesize fetchedResultsController, managedObjectContext,AddToDoButton;


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad 
  [super viewDidLoad];
  [self setTitle:@"Today"];
  [[self navigationItem] setRightBarButtonItem:[self editButtonItem]];





  NSError *error = nil;
  if (![[self fetchedResultsController] performFetch:&error])
  
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  

- (void) viewWillAppear:(BOOL)animated
    [self.tableView reloadData];

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath

  NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
  [[cell textLabel] setText:[[managedObject valueForKey:@"thingName"] description]];
  [[cell detailTextLabel] setText:[[managedObject valueForKey:@"thingDescription"] description]];
  [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.textLabel.textColor = [UIColor blueColor];
    cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:16.0f];
    cell.detailTextLabel.font = [UIFont fontWithName:@"Noteworthy" size:14.0f];


#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

  return [[fetchedResultsController sections] count];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

  id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
  return [sectionInfo numberOfObjects];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil)
  
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                   reuseIdentifier:@"Cell"] autorelease];
  

  [self configureCell:cell atIndexPath:indexPath];

  return cell;


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

  if (editingStyle == UITableViewCellEditingStyleDelete)
  
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

    NSError *error = nil;
    if (![context save:&error])
    
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      abort();
    
     


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

  return YES;


- (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
      toIndexPath:(NSIndexPath *)destinationIndexPath;
  
  NSMutableArray *things = [[fetchedResultsController fetchedObjects] mutableCopy];

  // Grab the item we're moving.
  NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];

  // Remove the object we're moving from the array.
  [things removeObject:thing];
  // Now re-insert it at the destination.
  [things insertObject:thing atIndex:[destinationIndexPath row]];

  // All of the objects are now in their correct order. Update each
  // object's displayOrder field by iterating through the array.
  int i = 0;
  for (NSManagedObject *mo in things)
  
    [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
  

  [things release], things = nil;

  [managedObjectContext save:nil];







#pragma mark -
#pragma mark Fetched results controller
- (IBAction)AddToDoAction:(id)sender 

    AddToDoViewController *viewController = [[AddToDoViewController alloc] init];
    [self presentViewController:viewController animated:YES completion:nil];



- (NSFetchedResultsController *)fetchedResultsController

  if (fetchedResultsController) return fetchedResultsController;

  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = 
               [NSEntityDescription entityForName:@"FavoriteThing" 
                           inManagedObjectContext:managedObjectContext];

  [fetchRequest setEntity:entity];

  NSSortDescriptor *sortDescriptor = 
              [[NSSortDescriptor alloc] initWithKey:@"displayOrder" 
                                          ascending:YES];

  NSArray *sortDescriptors = [[NSArray alloc] 
                              initWithObjects:sortDescriptor, nil];  
  [fetchRequest setSortDescriptors:sortDescriptors];

  NSFetchedResultsController *aFetchedResultsController = 
              [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                  managedObjectContext:managedObjectContext
                                                    sectionNameKeyPath:nil cacheName:@"ThingsCache"];
  aFetchedResultsController.delegate = self;
  [self setFetchedResultsController:aFetchedResultsController];

  [aFetchedResultsController release];
  [fetchRequest release];
  [sortDescriptor release];
  [sortDescriptors release];

  return fetchedResultsController;
    

- (void)dealloc 
  [fetchedResultsController release];
  [managedObjectContext release];


  [super dealloc];



@end

这是来自第二个视图控制器的代码

#import "AddToDoViewController.h"

#import "FavoriteThing.h"
#import "AppDelegate.h"
#import "RootViewController.h"

@interface AddToDoViewController ()

@end

@implementation AddToDoViewController
@synthesize ToDoDescriptionTextField,ToDoTextField,fetchedResultsController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [ToDoTextField becomeFirstResponder];



    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.




- (IBAction)BackButtonAction:(id)sender 

     [self dismissModalViewControllerAnimated:YES];



- (IBAction)SaveButtonAction:(id)sender 


    AppDelegate* appDelegate = [AppDelegate sharedAppDelegate];
    NSManagedObjectContext* context = appDelegate.managedObjectContext;

    NSManagedObject *favoriteThing = [NSEntityDescription insertNewObjectForEntityForName:@"FavoriteThing" inManagedObjectContext:context];


    [favoriteThing setValue:@"ToDo 000250" forKey:@"thingName"];
    [favoriteThing setValue:@"It sold out in eight days this year, you know?" forKey:@"thingDescription"];
    [favoriteThing setValue:[NSNumber numberWithInt:0] forKey:@"displayOrder"];




    NSError *error;
    if(! [context save:&error])
    
        NSLog(@"Whoopw,couldn't save:%@", [error localizedDescription]);
    
                               
@end

【问题讨论】:

使用自定义委托来传递数据。并在自定义委托中重新加载您的表格查看数据.... @mvasco- 在 vi​​ewWillAppear 中重新加载 谢谢@Seeker,但你的提议能更清楚一点吗? 谢谢@VivekSehrawat,请看我对user203108的评论 【参考方案1】:

在 viewWillAppear 中重新加载您的 tableview。

-(void)viewWillAppear:(BOOL)animated



NSError *error = nil;
  if (![[self fetchedResultsController] performFetch:&error])
  
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
  
[self.tableView reloadData];

【讨论】:

谢谢@user203108,已经按照你说的完成了,tableview没有更新,我想我必须更新获取请求而不是tableview本身.. 如果你实现了这个答案,你不应该为 NSFetchedResultsController 烦恼,因为你没有实现委托。【参考方案2】:

由于您使用的是 NSFetchedResultsController,您应该使用它的委托来更新您的表格。

否则你不需要使用 NSFetchedResultsController。有关实现 NSFetchedResultsController 委托的信息please see this link.

【讨论】:

谢谢@bbarnhart,但我已经接受了user203108的回答。【参考方案3】:

在你的 addVC 中声明这个自定义委托

@protocol AddViewControllerDelegate <NSObject>

-(void)AddPhotoViewController:(AddViewController *)addViewController store:(NSString *) data;

@end

并在 addVC 中将自定义委托声明为属性变量

@property(strong, nonatomic) id <AddViewControllerDelegate> delegate;

在 mainVC 中这样做

同时推送 addVC 像这样

AddPhotoViewController*   addVC = [[AddPhotoViewController alloc]init];
addVC.delegate = self;
[self.navigationController pushViewController:addVC animated:YES];


-(void)AddPhotoViewController:(AddPhotoViewController *)addViewController store:(NSString *)data

    [table_array addObject:data];
    [self.tableView reloadData];

【讨论】:

以上是关于从另一个视图重新加载表视图的主要内容,如果未能解决你的问题,请参考以下文章

使用标签栏从另一个视图返回时如何重新加载视图控制器

如何快速从另一个视图控制器重新加载表格视图

如何通过iOS Objective C中的appDelegate从另一个视图控制器重新加载表视图

swift:重新加载视图以在图表中显示新数据

如何从另一个 UIViewController 重新加载 UITableViewController

快速从另一个视图控制器更改 webview 的 url