【发布时间】:2012-06-05 12:21:03
【问题描述】:
我有一个遵循这个的模型:
然后我想从UITableView 入库一个城市。问题是,我想将它们保存在 iPhone(数据库)中直到达到 10,然后它会删除第一个并添加最后一个。我有这个想法,但我不能随便添加任何东西,我很困惑。
当我单击一个城市时,它会推送到下一个视图,从响应 JSON 中发送城市的 ID 和名称.. 没关系。但我想存储这些以便进行相同的呼叫,但来自另一个 UITableView(在它下面)所以它就像“搜索的最后城市”之类的东西。
我想保存 id、name,然后将其加载到仅显示名称的另一个 tableView 中。有了这个,我很好,但我无法达到库存的目的。
代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailTaxi *detailView = [[DetailTaxi alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
NSString *nomville = [[_jsonDict objectAtIndex:indexPath.row] objectForKey:@"label"];
NSString *idVille = [[_jsonDict objectAtIndex:indexPath.row] objectForKey:@"id"];
detailView.title = nomville;
detailView.idVille = idVille;
NSLog(@"Valor: %@", nomville);
NSLog(@"Valor: %@", idVille);
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSString *insert = [[NSString alloc] initWithFormat:@"%@,%@",idVille,nomville];
NSManagedObject *newVille;
NSArray *insertVilles = [insert componentsSeparatedByString:@","];
for(NSString *insert in insertVilles) {
newVille = [NSEntityDescription insertNewObjectForEntityForName:@"Ville" inManagedObjectContext:context];
[newVille setValue:[insertVilles objectAtIndex:0] forKey:@"id"];
[newVille setValue:[insertVilles objectAtIndex:1] forKey:@"nom"];
}
[self.navigationController pushViewController:detailView animated:YES];
}
【问题讨论】:
标签: iphone ios xcode core-data