sqlite数据更新从一个视图到另一个视图

时间:2014-06-30 10:56:03

标签: ios objective-c sqlite uitableview

嗨,我正在开发面向电子商务,这是我第一次面临一些问题。我在UITableView控制器中有一个产品列表。

enter image description here

我有上面图片中的产品列表,我可以增加产品添加到购物车的数量 在我的添加中,我给出了更新和取消等选项。在我的产品列表中,当用户单击添加按钮时,就像这样。

enter image description here

当用户点击添加UIbutto时,它将存储在我的本地数据库sqlite中,它将显示在我的购物车列表中。

现在的问题是我提供选项,如在购物车本身,他们可以取消或更新订单,如果用户取消购物车中的订单,它不在我的产品列表中更新它仍然显示添加的产品和显示取消按钮,如同图像。

如果我点击图片显示空错误现在我想要做的就是如果用户取消我的产品列表中的购物车列表中的订单它显示添加选项不取消请告诉我该怎么做。

我的代码我在产品列表的UIButton中动态创建UITableView

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     static NSString *cellIdentifier =@"Cell";


     meunuCell *cell =(meunuCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {

       cell = [[meunuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }


     buon2 = [UIButton buttonWithType:UIButtonTypeCustom];
     buon2.frame = CGRectMake(264,20, 32, 32);
     buon2.tag=indexPath.row;


 if ([[count objectAtIndex:indexPath.row] isEqualToString:@"yes"]) {


    [buon2 setImage:[UIImage imageNamed:@"deletes.png"] forState:UIControlStateNormal];

     }
 else{
      if ([[count objectAtIndex:indexPath.row] isEqualToString:@"no"]) {


        [buon2 setImage:[UIImage imageNamed:@"inserts.png"] forState:UIControlStateNormal];
      }

   }
      [buon2 addTarget:self action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
      [cell.contentView addSubview:buon2];

插入并删除我的产品清单中的代码。

  -(void)deletedata:(NSString*)query{
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];
       NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"bp.sqlite"];
        sqlite3_stmt *update_statement;
   if(sqlite3_open([databasePath UTF8String], &_myDataBase) == SQLITE_OK)
    {
        const char *insert_stmt = [query UTF8String];
        sqlite3_prepare_v2(_myDataBase,insert_stmt, -1, &update_statement, NULL);
      if(sqlite3_step(update_statement)==SQLITE_DONE)
      {
        NSLog(@"Delete successfully");
      }
     else
      {
          NSLog(@"Delete not successfully");

      }
      sqlite3_finalize(update_statement);
      sqlite3_close(_myDataBase);
    }

  }


 -(void)viewWillAppear:(BOOL)animated
 {


   pdi=[[NSMutableArray alloc]init];
   [self openDB];
   NSString *sql =[NSString stringWithFormat:@"SELECT * FROM br"];
   sqlite3_stmt *statement;
   if (sqlite3_prepare_v2(_myDataBase, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
          while (sqlite3_step(statement)== SQLITE_ROW) {

          char *field1 = (char *)sqlite3_column_text(statement, 0);
          NSString *field1Str =[[NSString alloc]initWithUTF8String:field1];

          char *field2 = (char *)sqlite3_column_text(statement, 1);
          NSString *field2Str =[[NSString alloc]initWithUTF8String:field2];

          NSString *str = [[NSString alloc]initWithFormat:@"%@",field2Str];
          NSLog(@"value%@",str);
          NSLog(@"pdi%@",field1Str);
          [pdi addObject:str];
       }

     } 
   }


  -(void)aMethod2:(UIButton*)sender
   {

 if ([[count objectAtIndex:sender.tag] isEqualToString:@"yes"]) {
      NSLog(@"deleted");
      int tag=[[sender superview] tag];
      NSString  *sql = [NSString stringWithFormat:@"delete from br WHERE PID='%@'",[pdi objectAtIndex:tag]];
      [self deletedata:sql];
      NSLog(@"your deletion code here");
      [self.mytableview reloadData];
      [count replaceObjectAtIndex:sender.tag withObject:@"no"];
   }

 else
  {
      NSLog(@"inserted");

     //call your insert method
      NSString *inr=[intarr objectAtIndex:sender.tag];
      NSLog(@"%@",inr);
      NSString *item=[self.menuarray objectAtIndex:sender.tag];
      NSLog(@"%@",item);
      NSString *cost=[self.menuarray1 objectAtIndex:sender.tag];
      NSLog(@"%@",cost);
      NSString *pid =[self.menuarray2 objectAtIndex:sender.tag];
      NSLog(@"%@",pid);
      int qt = [inr intValue];
      int mn =  [cost intValue];
      int total = qt * mn;
      tot =[NSString stringWithFormat:@"%d",total];
      NSString *sql = [NSString stringWithFormat:@"INSERT INTO br ('PID','NAME','COST','QUTY','TOTAL') VALUES ('%@','%@','%@','%@','%@')",pid,item,cost,inr,tot];
       NSLog(@"%@",sql);
      [sender setSelected:YES];

      [count addObject:@"yes"];

      [self Insertdata:sql];

      [count replaceObjectAtIndex:sender.tag withObject:@"yes"];
      [self.mytableview reloadData];

    } 

  }

我已经在我的产品列表中使用了上面的代码进行插入和删除,其中如何添加代码,例如在我的购物车列表中删除产品时我希望在我的产品列表中显示添加选项我还没有得到任何解决方案请帮帮我。

enter image description here

enter image description here

0 个答案:

没有答案