关于stacoverflow有两个类似的问题,我的问题类似于this,但现在我使用Xcode 5,我的代码不同。我尝试了很多不同的方法,但它们不起作用。在网络上没有解决方案。
使用核心数据,我有一个带有添加按钮的表视图控制器,它可以模态调用一个新的视图控制器,提示用户将文本添加到1个字段中。当用户单击“保存”时,该条目将作为标题单元格添加到表视图控制器中,并填入信息。 我感到困惑并且一直在寻找答案的事实是每个新条目都被添加到单元格的底部,因此如果有足够的单元格填充屏幕,则新添加的条目将添加到底部。 我真的希望将每个新条目都放到表格视图单元格的顶部。
我的表格视图:
-(NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (IBAction)cancel:(id)sender {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *fechRequest = [[NSFetchRequest alloc] initWithEntityName:@"OneMoreCoffe"];
moreCoffe = [[moc executeFetchRequest:fechRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return moreCoffe.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
NSManagedObject *differentCoffe = [moreCoffe objectAtIndex:indexPath.row];
cell.textLabel.text = [differentCoffe valueForKey:@"type"];
return cell;
}
我的视图控制器:
@implementation DataViewController
@synthesize textFieldType,differentCoffe;
-(NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSManagedObject *newCoffe = [NSEntityDescription insertNewObjectForEntityForName:@"OneMoreCoffe" inManagedObjectContext:context];
[newCoffe setValue:textFieldType.text forKey:@"type"];
[context save:&error];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)cancel:(id)sender {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
if (differentCoffe) {
[textFieldType setText:[differentCoffe valueForKey:@"type"]];
}
}
答案 0 :(得分:0)
如果你不介意他们总是处于相反的顺序,你可以只在顶部显示最新的。
替换它:
NSManagedObject *differentCoffe = [moreCoffe objectAtIndex:indexPath.row];
用这个:
NSInteger rowCount = [self.tableView numberOfRowsInSections:indexPath.section]
NSManagedObject *differentCoffe = [moreCoffe objectAtIndex:(rowCount - 1 - indexPath.row)];