gpt4 book ai didi

ios - 从另一个 View 重新加载 TableView

转载 作者:行者123 更新时间:2023-11-28 20:10:27 25 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我需要从另一个 View 更新一个 TableView 。我试过使用 NSNotifications 但没有成功。第一个 View 包含一个使用获取请求从核心数据填充的 TableView 。此 View 还有一个添加按钮,可打开一个 View 以将对象添加到核心数据实体。第二个 View 有多个文本字段、一个保存按钮和一个可再次打开第一个 View 的后退按钮,但行不会更新。这是来自第一个 View Controller 的代码:

#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

这是来自第二个 View Controller 的代码

#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

最佳答案

在 viewWillAppear 中重新加载您的表格 View 。

-(void)viewWillAppear:(BOOL)animated
{


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

关于ios - 从另一个 View 重新加载 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20486069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com