Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moumen

macrumors newbie
Original poster
Dec 8, 2011
1
0
Hi,
I'm working on implementation of reservation specials restaurants
but I have a problem of duplication of calculating the total.
so when i want to add any thinks to this View so the total will increased.
I try to find a solution, but .... : ( .
I put images for this problem.

Code:
//
//  ShowDestinationsViewController.m
//  CoreDataTabBarTutorial
//
//  Created by Kent Franks on 7/5/11.
//  Copyright 2011 TheAppCodeBlog. All rights reserved.
//
 
#import "ShoppingCartViewController.h"
#import "Monresto.h"
#import "EditQuantiteViewController.h"
#import "DetailsplatViewController.h"
 
 
@interface ShoppingCartViewController ()
@property (nonatomic, retain) ReusableDatePickerViewController *reusableDatePicker;
@end
 
@implementation ShoppingCartViewController
@synthesize dateLabel, reusableDatePicker,timeLabel, selectedTime;
@synthesize destinationsTableView, destinationsArray;
@synthesize fetchedResultsController,labelTotal,totalView, managedObjectContext;
@synthesize editQuantiteViewController,totalgenerale1,totalgenerale2,totalgenerale;
//@synthesize selectedMonrestoIndex;
///////////////////
#pragma mark -
#pragma mark View lifecycle
 
- (void)redrawDate
{
//NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
 
//timeLabel.text = [NSString stringWithFormat:@"%@",
//[dateFormatter stringFromDate:selectedDate],
// [timeFormatter stringFromDate:selectedTime]];
//timeLabel.text = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle];
[timeFormatter release];
//[dateFormatter release];   
}
 
- (IBAction)changeDate:(id)sender
{
DateTimePicker *controller = [[DateTimePicker alloc] init];
controller.delegate = self;
controller.defaultDate = selectedTime;
[self presentModalViewController:controller animated:YES];
[controller release];
}
 
- (void)dateTimePickerOK:(DateTimePicker *)controller didPickDate:(NSDate *)date
{
timeLabel.text = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
 
self.selectedTime = date;
[self redrawDate];
 
[controller dismissModalViewControllerAnimated:YES];
}
 
- (void)dateTimePickerCancel:(DateTimePicker *)controller
{
[controller dismissModalViewControllerAnimated:YES];
}
 
 
 
 
 
-(void) someMethodUsingUsername {
//LoginViewController *c1 = [[LoginViewController alloc] init];
NSString *theTtc = [DetailsplatViewController ttc]; // This is how to call a Class Method in Objective C
NSLog(@"theTtc: %@", theTtc);
 
 
}
 
 
 
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Panier";
//NSString *theTtc = [DetailsplatViewController ttc]; // This is how to call a Class Method in Objective C
//NSLog(@"theTtc %@", theTtc);
ReusableDatePickerViewController *r = [[ReusableDatePickerViewController alloc] initWithNibName:nil bundle:nil];
self.reusableDatePicker = r;
[r release];
 
reusableDatePicker.delegate = self;
 
dateLabel.text = @"";
 
self.selectedTime = [NSDate date];
[self redrawDate];
//[self.view addSubview:totalView];
//NSFetchRequest *request = [[NSFetchRequest alloc] init];
//NSEntityDescription *entity = [NSEntityDescription entityForName:@"Destinations"inManagedObjectContext:managedObjectContext];
//[request setEntity:entity];
 
//for (int i = 0; i < [destinationsArray count]; i++) {
/*
/*
for (int i = 0; i < [destinationsArray count]; i++) {
for (NSDictionary *q in destinationsArray)
totalgenerale +=(float)[q objectForKey:@"totalp"];
//totalgenerale += (Monresto *)[destinationsArray objectAtIndex:i];
NSLog(@"totalgenerale", totalgenerale);
 
*/
 
 
//}
//NSError *error;
//NSMutableArray *mutableFetchResults =[[managedObjectContextexecuteFetchRequest:request error:&error] mutableCopy];
//if (mutableFetchResults == nil) {
// Gestion de l’erreur
//}
//self.destinationsArray = mutableFetchResults;
//[mutableFetchResults release];
//[request release];
 
//destinationsArray = [[NSMutableArray alloc] init];
}
 
 
- (IBAction)showDatePicker:(id)sender
{
[self presentModalViewController:reusableDatePicker animated:YES];
}
 
- (void)reusableDatePickerDidCancel:(ReusableDatePickerViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
 
- (void)reusableDatePicker:(ReusableDatePickerViewController *)controller didPickDate:(NSDate *)date
{
dateLabel.text = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle];
[self dismissModalViewControllerAnimated:YES];
}
 
 
 
 
 
- (void) viewWillAppear:(BOOL)animated
{
 
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Destinations" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
 
if (mutableFetchResults == nil)
{
// Handle the error.
NSLog(@"mutableFetchResults == nil");
}
[self setDestinationsArray:mutableFetchResults];
/*for (int i = 0; i < [destinationsArray count]; i++) {
 
{totalgenerale =[destinationsArray objectAtIndex:4];
totalgenerale1 += [totalgenerale floatValue];
 
//totalgenerale += (Monresto *)[destinationsArray objectAtIndex:i];
}}
NSLog(@"totalgenerale %@", totalgenerale);
NSLog(@"totalgenerale1 is: %0.3f", totalgenerale1);*/
[request release];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
 
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
 
for (NSManagedObject *info in fetchedObjects)
{
 
totalgenerale =[info valueForKey:@"totalp"];
 
//NSLog(@"totalgenerale",totalgenerale);
totalgenerale1 += [totalgenerale floatValue];
 
}
 
[fetchRequest release];
 
NSLog(@"totalgenerale %@", totalgenerale);
NSLog(@"totalgenerale1 is: %0.3f", totalgenerale1);
totalgenerale2 = [NSString stringWithFormat:@"Total: %0.3f DT", totalgenerale1];
 
// totalgenerale1 = [[[totalgenerale text] stringByReplacingOccurrencesOfString:@"$" withString:@""] floatValue];   
/*
Place the title label.
place the label whatever the current X is plus 10 pixels from the left
place the label 4 pixels from the top
make the label 200 pixels wide
make the label 20 pixels high
*/
//labelTotal.text =[NSString stringWithFormat:@"%0.3f",totalgenerale1];
//labelTotal.text =totalgenerale;
//self.title = [NSString stringWithFormat:@"%0.3f",totalgenerale1];
//NSLog(@"azert %@", labelTotal.text);
 
UILabel *label3 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 28)] autorelease];
label3.backgroundColor = [UIColor grayColor];
label3.font = [UIFont systemFontOfSize:15];
label3.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8];
label3.textAlignment = UITextAlignmentRight;
label3.textColor = [UIColor whiteColor];
label3.text = totalgenerale2;
destinationsTableView.tableFooterView = label3;
 
[destinationsTableView reloadData];
}
 
#pragma mark -
#pragma mark Table view data source
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
 
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [destinationsArray count];
}
 
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
static NSString *CellIdentifier = @"Cell";
 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
 
Monresto * monresto = [destinationsArray objectAtIndex:indexPath.row];
cell.textLabel.text=monresto.name;
cell.detailTextLabel.text=monresto.totalp;
 
NSString *total1 = monresto.totalp;
 
NSLog (@"total1", total1);
//float totalp = [total1 floatValue];
// myIntprix = [stringprix intValue];
 
// NSLog (@"%i", myIntprix);
//NSLog(@"totalp is: %0.3f", totalp);
 
 
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//cell.imageView.image = [UIImage imageNamed:@"arrow_right.png"];
 
//cell.
//cell.detailTextLabel.text=monresto.quantite;
//cell.text= monresto.quantite;
 
 
return cell;
}
 
/////////////////////
 
 
 
 
/////////////////////
 
 
 
#pragma mark -
#pragma mark Table view delegate
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 
if (self.editQuantiteViewController == nil)
{
EditQuantiteViewController *temp = [[EditQuantiteViewController alloc] initWithNibName:@"EditQuantiteViewController" bundle:[NSBundle mainBundle]];
self.editQuantiteViewController = temp;
[temp release];
}
 
self.editQuantiteViewController.managedObjectContext = self.managedObjectContext;
Monresto *monresto = (Monresto *)[destinationsArray objectAtIndex:indexPath.row];
self.editQuantiteViewController.monresto = monresto;
 
[self.navigationController pushViewController:self.editQuantiteViewController animated:YES];
 
}
 
 
 
 
 
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
 
// Release any cached data, images, etc. that aren't in use.
}
 
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
 
 
- (void)dealloc
{
[destinationsArray release];
[destinationsTableView release];
[reusableDatePicker release];
[dateLabel release];
[super dealloc];
}
 
 
@end
 

Attachments

  • image 1.png
    image 1.png
    361.4 KB · Views: 65
  • image 2.png
    image 2.png
    398.9 KB · Views: 74
  • image 3.png
    image 3.png
    399.5 KB · Views: 63
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.