在此,我点击更新按钮,我的价格没有立即更新,当我回到家庭视图控制器时它更新。请帮帮我。[我想点击更新按钮,我的产品价格会根据1公斤,2公斤,5公斤等数量的选择而变化]
#import "MainCartViewController.h"
#import "CartTableCell.h"
#import "CheckOutViewController.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "Detail.h"
#import "MBProgressHUD.h"
#define JSON_URL @"http://www.webchefz.com/Andriod/index.php?Table=Cart&UserID=11"
@interface MainCartViewController ()
@end
@implementation MainCartViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
UINavigationBar *navBar = [[self navigationController] navigationBar];
[ navBar setTintColor:[UIColor whiteColor]];
UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom];
[Btn setFrame:CGRectMake(0.0f,0.0f,30.0f,30.0f)];
[Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"carc.png"]] forState:UIControlStateNormal];
[Btn addTarget:self action:@selector(AddtoCartButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn];
cFRAIPArray = [[NSMutableArray alloc]init];
tableData =[[NSMutableArray alloc]init];
blogURL= [NSURL URLWithString:JSON_URL];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
dataDictionary = [NSJSONSerialization
JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
tableData = [dataDictionary objectForKey:@"userlist"];
// int tp = [NSString stringWithFormat:@"%d",[dataDictionary valueForKey:@"TotalPrice"]];
totalPrice= [[dataDictionary objectForKey:@"userlist"]valueForKey:@"TotalPrice"];
NSLog(@"TP IS %@", [dataDictionary valueForKey:@"TotalPrice"]);
subtotal.text= [[NSString alloc] initWithFormat:@"%@ Rs.",[dataDictionary valueForKey:@"TotalPrice"]];
int ShippingValue = [[subtotal text] intValue];
int value = 50;
float floatNum = ShippingValue + value;
grandTotal.text=[[NSString alloc] initWithFormat:@"%.1f Rs.", floatNum];
NSLog(@"dwfefefeusers are %@", totalPrice);
NSLog(@"dwfefefeusers are %@", tableData);
NSString *items= [[dataDictionary objectForKey:@"userlist"]valueForKey:@"Name"];
NSLog(@"hjdbhsdbchjds%@",items
);
for (dataDictionary in tableData)
{
// currentHotel= [[Detail alloc]initWithId:[[bpDictionary objectForKey:@"id"]integerValue] Name:[bpDictionary objectForKey:@"Name"] HindiName:[bpDictionary objectForKey:@"HindiName"] MRP:[[bpDictionary objectForKey:@"MRP"]integerValue] ImagePath:[bpDictionary objectForKey:@"ImagePath"]Category:[bpDictionary objectForKey:@"Category"]];
currentHotel = [[Detail alloc]initWithId:[[dataDictionary objectForKey:@"ID"]integerValue] Name:[dataDictionary objectForKey:@"Name"] HindiName:[dataDictionary objectForKey:@"HindiName"] MRP:[[dataDictionary objectForKey:@"TotalPrice"]integerValue] CartID:[[dataDictionary objectForKey:@"CartID"]integerValue] ProductID:[[dataDictionary objectForKey:@"ProductID"]integerValue] Quantity:[[dataDictionary objectForKey:@"Quantity"]integerValue] userName:[dataDictionary objectForKey:@"UserName"] ImagePath:[dataDictionary objectForKey:@"ImagePath"] Category:[dataDictionary objectForKey:@"Category"]];
[self.objectHolderArray addObject:currentHotel];
}
CGRect frame = CGRectMake(0, 0, 0, 44);
UILabel *label = [[UILabel alloc]initWithFrame:frame];
label.text = @"Cart List";
label.textColor=[UIColor whiteColor];
label.font = [UIFont fontWithName:@"Helvetica Neue" size:16];
self.navigationItem.titleView = label;
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Initialize table data
// tableData = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
}
-(NSMutableArray *)objectHolderArray
{
if(!_objectHolderArray) _objectHolderArray = [[NSMutableArray alloc]init];
return _objectHolderArray;
}
IBAction)AddtoCartButton:(id)sender
{
MainCartViewController *cartView= [[MainCartViewController alloc]initWithNibName:@"MainCartViewController" bundle:nil];
[self.navigationController pushViewController:cartView animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CartTableCell";
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CartTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
currentHotel = [self.objectHolderArray
objectAtIndex:indexPath.item];
NSLog(@"%@",[self.objectHolderArray
objectAtIndex:indexPath.item] );
cell.nameLabel.text=currentHotel.name;
cell.MRPLabel.text=[NSString stringWithFormat:@"Rs.%d",currentHotel.MRP];
NSLog(@"price is %@",cell.MRPLabel.text);
[cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:currentHotel.images] placeholderImage:[UIImage imageNamed:@"defaultPlayerImge.png"] options:SDWebImageRefreshCached|SDWebImageProgressiveDownload];
[cell.deleteBtn addTarget:self action:@selector(deleteProduct:) forControlEvents:UIControlEventTouchUpInside];
cell.deleteBtn.tag = [indexPath row];
[cell.PickerBtn addTarget:self action:@selector(createPickerView:) forControlEvents:UIControlEventTouchUpInside];
cell.PickerBtn.tag = [indexPath row];
[cell.updateBtn addTarget:self action:@selector(updateProduct:) forControlEvents:UIControlEventTouchUpInside];
cell.updateBtn.tag = [indexPath row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (debtn.deleteBtn.highlighted) {
NSLog(@"cff");
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.objectHolderArray count];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
int errorCode = httpResponse.statusCode;
NSString *fileMIMEType = [[httpResponse MIMEType] lowercaseString];
NSLog(@"response is %d, %@", errorCode, fileMIMEType);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"data is %@", data);
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string is %@", myString);
NSError *e = nil;
flickrDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
NSLog(@"dictionary is %@", flickrDictionary);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[HUD hide:YES];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Quanity updated succesfully" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[self.tableView1 reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView1 reloadData];
// }
NSLog(@"cfraiparray %@", cFRAIPArray);
NSLog(@"Succeeded!");
}
-(void)createPickerView :(UIButton *)button
{
currentHotel=[self.objectHolderArray objectAtIndex:button.tag];
self.pickerViewTextField = [[UITextField alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.pickerViewTextField];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
weights= [[NSMutableArray alloc] initWithObjects:@"1 Kg",@"2 Kg",@"5 Kg",@"10 Kg",@"50 Kg",
nil];
// set change the inputView (default is keyboard) to UIPickerView
self.pickerViewTextField.inputView = pickerView;
// add a toolbar with Cancel & Done button
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
self.pickerViewTextField.inputAccessoryView = toolBar;
[self.pickerViewTextField becomeFirstResponder];
// [self.view addSubview:pickerView];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
// [self bringUpPickerViewWithRow:indexPath];
}
-(void) deleteProduct:(UIButton *)button
{
currentHotel=[self.objectHolderArray objectAtIndex:button.tag];
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.webchefz.com/Andriod/delete.php?Table=Cart&CartID=%d&UserID=11",currentHotel.CartID]];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:myRequest delegate:self];
NSLog(@"main cart id is %@", [NSString stringWithFormat:@"%d",currentHotel.CartID]);
NSLog(@"ded");
[self.objectHolderArray removeObjectAtIndex:button.tag];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
[self.tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView1 reloadData];
}
-(void) updateProduct:(UIButton *)button
{
HUD= [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = @"Please Wait...";
HUD.mode = MBProgressHUDModeIndeterminate;
[self.view addSubview:HUD];
[HUD show:YES];
currentHotel=[self.objectHolderArray objectAtIndex:button.tag];
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.webchefz.com/Andriod/update.php?Table=Cart&CartID=%d&Quantity=%d&UserID=11&ProductID=%d",currentHotel.CartID,pickerQuantity,currentHotel.ProductID]];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:myRequest delegate:self];
NSLog(@"main cart id is %@", [NSString stringWithFormat:@"%d",currentHotel.ProductID]);
NSLog(@"ded%@",[NSString stringWithFormat:@"%d",currentHotel.CartID]);
// [self.tableView1 insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
// [self.objectHolderArray removeObjectAtIndex:button.tag];
self.tableView1.delegate = self;
self.tableView1.dataSource = self;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
[self.tableView1 reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView1 reloadData];
}
- (void)cancelTouched:(UIBarButtonItem *)sender
{
// hide the picker view
[self.pickerViewTextField resignFirstResponder];
}
- (void)doneTouched:(UIBarButtonItem *)sender
{
// hide the picker view
[self.pickerViewTextField resignFirstResponder];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSIndexPath *indexPath ;
if ([self.objectHolderArray objectAtIndex:indexPath.row])
{
if ([[weights objectAtIndex:row]isEqual:@"1 Kg"])
{
pickerQuantity=1;
[self.pickerViewTextField becomeFirstResponder];
[self.pickerViewTextField becomeFirstResponder];
selectWeight.hidden=YES;
[self.tableView1 reloadData];
}
else if ([[weights objectAtIndex:row]isEqual:@"2 Kg"])
{
pickerQuantity=2;
selectWeight.hidden=YES;
[self.pickerViewTextField becomeFirstResponder];
[self.tableView1 reloadData];
}
else if ([[weights objectAtIndex:row]isEqual:@"5 Kg"]) {
pickerQuantity=5;
[self.pickerViewTextField becomeFirstResponder];
selectWeight.hidden=YES;
[self.tableView1 reloadData];
}
else if ([[weights objectAtIndex:row]isEqual:@"10 Kg"]) {
pickerQuantity=10;
[self.pickerViewTextField becomeFirstResponder];
selectWeight.hidden=YES;
[self.tableView1 reloadData];
}
else{
pickerQuantity=50;
selectWeight.hidden=YES;
[self.tableView1 reloadData];
}}
selectWeight.hidden=YES;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
//---number of items(rows) in the Picker View---
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [weights count];
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
// self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 30)];
// [self.view addSubview:selectWeight];
//
//
// // Get the text of the row.
NSString *rowItem = [weights objectAtIndex: row];
////
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];
[lblRow setTextAlignment:UITextAlignmentCenter];
[lblRow setTextColor: [UIColor blackColor]];
[lblRow setText:rowItem];
lblRow.font = [UIFont fontWithName:@"Verdana" size:12];
[lblRow setBackgroundColor:[UIColor clearColor]];
return lblRow;
}
@end
答案 0 :(得分:0)
你应该只调用[tableView reloadData],每次都在表视图中更新信息。