gpt4 book ai didi

ios - 使用核心数据刷新 TableView

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:06 25 4
gpt4 key购买 nike

我在插入新项目后重新加载我的核心数据时遇到问题我尝试了以下方法,但没有成功。我想知道这是否可能是由于我调用数据的方式造成的?我对此很陌生,并且一直在按照各种教程走到这一步,但是我现在卡住了。我知道 viewdidappear 正在被调用,因为 nslog 说明了这一点,但是表没有更新

`- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.tableView reloadData];

NSLog (@"did appear");
}`

有什么想法吗?

//  ViewController.m
// Core Data

#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UINavigationItem *back;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *name;
@property (strong, nonatomic) NSMutableArray *phone;

@end

@implementation ViewController


- (void)viewDidLoad
{
[super viewDidLoad];



self.name = [[NSMutableArray alloc] init];
self.phone = [[NSMutableArray alloc] init];

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
// NSPredicate *pred =[NSPredicate predicateWithFormat:@"(name = %@)", @"Vea Software"];
// [request setPredicate:pred];
NSManagedObject *matches = nil;

NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];

if ([objects count] == 0)
{
NSLog(@"No matches");
}
else
{
for (int i = 0; i < [objects count]; i++)
{
matches = objects[i];
[self.name addObject:[matches valueForKey:@"name"]];
[self.phone addObject:[matches valueForKey:@"phone"]];
}
}

}

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.tableView reloadData];

NSLog (@"did appear");
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



- (IBAction)add:(id)sender
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context];

addentry = self.addtextbox.text;

[newContact setValue: addentry forKey:@"name"];
[self.name addObject: addentry];
[newContact setValue: @"(555) 555 - 5555" forKey:@"phone"];
[self.phone addObject:@"(555) 555 - 5555"];
NSError *error;
[context save:&error];
[self.tableView reloadData];
}

#pragma Table View

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.name.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [self.name objectAtIndex:indexPath.row];
return cell;
}



@end

最佳答案

看看这个教程http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

您需要重新考虑 View Controller 的架构。一般来说,您想要:

  • 让 View Controller 加载数据,然后“坐在那里”等待接收事情发生变化的通知,并根据该通知重新加载。这是不应被数据访问操作阻塞的“UI 线程”

  • 让您的数据在后台发生变化。现代应用程序使用 Grand Central Dispatch 在后台进行这些操作,但还有其他替代方案,如 NSOperation、线程等。

  • NSFetchedResultsController 为您处理 UI 更新。让 Core Data context 管理数据整合。

这两个概念是让事情按预期运作的关键。否则,您可能会得到一堆代码。

祝你好运!

关于ios - 使用核心数据刷新 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22001602/

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