Search code examples
iosiphoneuitableviewuistepper

Json issue in tableview with stepper and lable


I am new in ios developer.

I want to selected data into other tableview my problem is i dont know how to get the name and quantity.

See here is my screen shot

enter image description here

My json data like this format

 {
        Data =     (
                    {
                "Dry_cleaning" = 49;
                Iron = 8;
                Name = "Shirt/Tees";
                Wash = 15;
                "Wash_Iron" = 19;
                id = 1;
            },
                    {
                "Dry_cleaning" = 49;
                Iron = 5;
                Name = "Kurta(short)";
                Wash = 20;
                "Wash_Iron" = 19;
                id = 2;
            },
                    {
                "Dry_cleaning" = 50;
                Iron = 8;
                Name = "Kurta(Long)";
                Wash = 15;
                "Wash_Iron" = 20;
                id = 3;
            },
                    {
                "Dry_cleaning" = 50;
                Iron = 5;
                Name = Shorts;
                Wash = 15;
                "Wash_Iron" = 19;
                id = 4;
            },
                    {
                "Dry_cleaning" = 55;
                Iron = 8;
                Name = "Trousers/Pyjamas";
                Wash = 20;
                "Wash_Iron" = 30;
                id = 5;
            },
                    {
                "Dry_cleaning" = 70;
                Iron = 7;
                Name = Jeans;
                Wash = 20;
                "Wash_Iron" = 29;
                id = 6;
            },
                    {
                "Dry_cleaning" = 150;
                Iron = 5;
                Name = Blazer;
                Wash = 25;
                "Wash_Iron" = 19;
                id = 7;
            },
                    {
                "Dry_cleaning" = 100;
                Iron = 6;
                Name = "Leather_Jacket";
                Wash = 30;
                "Wash_Iron" = 36;
                id = 8;
            }
}

Here this is my code

ViewController.h

import

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic)NSMutableArray *arrayCounts;
- (IBAction)placeorder:(id)sender;
@end

ViewController.m

    //
//  ViewController.m
//  orderscreenfile
//
//  Created by Tranetech on 02/02/16.
//  Copyright (c) 2016 Tranetech. All rights reserved.
//

#import "ViewController.h"
#import "custom.h"
@interface ViewController ()
{
    NSDictionary *dic_property;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSThread detachNewThreadSelector:@selector(fetchdata) toTarget:self withObject:nil];
    //[NSThread detachNewThreadSelector:@selector(arrycount) toTarget:self withObject:nil];

    self.arrayCounts = [NSMutableArray array];


    NSLog(@"array count=%d",self.arrayCounts.count);
    [self.tableview reloadData];
    // Do any additional setup after loading the view, typically from a nib.
}
//-(void)arrycount
//{
//    for (int i = 0; i<[[dic_property objectForKey:@"Data"] count]; i++) {
//        [self.arrayCounts insertObject:[NSString stringWithFormat:@"0"] atIndex:i];
//    }
// 
//}
-(void)fetchdata
{
    NSString *login= [[NSString stringWithFormat:@"http://men_dry.php"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"----%@", login);
    NSURL *url = [NSURL URLWithString:[login stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    //-- Get request and response though URL
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];


    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               dispatch_async(dispatch_get_main_queue(), ^{
                                   if (data) {
                                       dic_property= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                                       NSLog(@"%@", dic_property);
                                       NSLog(@"counts=%d",[[dic_property objectForKey:@"Data"]count]);
                                       for (int i = 0; i<[[dic_property objectForKey:@"Data"] count]; i++) {
                                                   [self.arrayCounts insertObject:[NSString stringWithFormat:@"0"] atIndex:i];
                                               }

                                       [self.tableview reloadData];
                                   }
                                   else {
                                       NSLog(@"network error, %@", [error localizedFailureReason]);
                                   }
                               });

                           }];


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayCounts.count;
   // return [[dic_property objectForKey:@"Data"]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    custom *cell;
    static NSString *simpleTableIdentifier = @"customcell";

    cell = (custom *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
        cell = [[[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil] objectAtIndex:0];

    cell.lblcount.text = self.arrayCounts[indexPath.row];
    cell.lbltitle.text=[[[dic_property objectForKey:@"Data"]objectAtIndex:indexPath.row]objectForKey:@"Name"];
    cell.lblcount.tag=indexPath.row;
    [cell.stepper addTarget:self action:@selector(itemsChange:) forControlEvents:UIControlEventValueChanged];
    return cell;
}
- (void)itemsChange:(UIStepper*)stepper
{
    CGPoint cursorPosition = [stepper convertPoint:CGPointZero toView:self.tableview];
    NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:cursorPosition];

    custom *currentCell = (custom *)[self.tableview cellForRowAtIndexPath:indexPath];


    currentCell.lblcount.text=[NSString stringWithFormat:@"%f",[stepper value]];

    [self.arrayCounts replaceObjectAtIndex:indexPath.row withObject:[NSString stringWithFormat:@"%f",[stepper value]]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)placeorder:(id)sender {
}
@end

I am stuck into three days please solve my problem Thanks in ADVANCE.


Solution

  • @Arpit replace your code with Arry.

    viewController.h


    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
    
    @property (weak, nonatomic) IBOutlet UITableView *tableview;
    @property (strong, nonatomic)NSMutableArray *arrayCounts;
    - (IBAction)placeorder:(id)sender;
    
    @property (weak, nonatomic) IBOutlet UIButton *placeorder;
    
    @end
    

    ViewController.m

    - (void)viewDidLoad {
        [super viewDidLoad];
        [NSThread detachNewThreadSelector:@selector(fetchdata) toTarget:self withObject:nil];
    
        self.arrayCounts = [NSMutableArray array];
        NSLog(@"array count=%lu",(unsigned long)self.arrayCounts.count);
    }
    
    -(void)fetchdata
    {
        NSArray *tempArray = @[@{@"Dry_cleaning" : @"49",
                                 @"Iron":@"5",
                                 @"Name":@"Shirt/Tees",
                                 @"Wash":@"20",
                                 @"Wash_Iron":@"19",
                                 @"id":@"1"},
                               @{@"Dry_cleaning" : @"49",
                                 @"Iron":@"5",
                                 @"Name":@"Kurta(short)",
                                 @"Wash":@"20",
                                 @"Wash_Iron":@"19",
                                 @"id":@"2"},
                               @{@"Dry_cleaning" : @"49",
                                 @"Iron":@"5",
                                 @"Name":@"Kurta(Long)",
                                 @"Wash":@"15",
                                 @"Wash_Iron":@"20",
                                 @"id":@"3"},
                               @{@"Dry_cleaning" : @"50",
                                 @"Iron":@"5",
                                 @"Name":@"Shorts",
                                 @"Wash":@"15",
                                 @"Wash_Iron":@"19",
                                 @"id":@"4"},
                               @{@"Dry_cleaning" : @"51",
                                 @"Iron":@"5",
                                 @"Name":@"Trousers/Pyjamas",
                                 @"Wash":@"20",
                                 @"Wash_Iron":@"19",
                                 @"id":@"5"}];
    
    
        // remove comment from below code and add where responce will come. -----------------
    
        // NSArray *tempArray = dic_property[@"Data"];
        [tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSMutableDictionary *dictValues  = [NSMutableDictionary dictionaryWithDictionary:obj];
            [dictValues setValue:@"0" forKey:@"counts"];
    
            [self.arrayCounts addObject:dictValues];
        }];
    
        [self.tableview reloadData];
        //--------------------------------
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.arrayCounts.count;
        // return [[dic_property objectForKey:@"Data"]count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        custom *cell;
        static NSString *simpleTableIdentifier = @"customcell";
    
        cell = (custom *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
        if (cell == nil)
            cell = [[[NSBundle mainBundle] loadNibNamed:@"custom" owner:self options:nil] objectAtIndex:0];
    
    
        cell.lblCount.text = self.arrayCounts[indexPath.row][@"counts"];
        cell.lbltitle.text=self.arrayCounts[indexPath.row][@"Name"];;
    
        [cell.stepper addTarget:self action:@selector(itemsChange:) forControlEvents:UIControlEventValueChanged];
        return cell;
    }
    - (void)itemsChange:(UIStepper*)stepper
    {
        CGPoint cursorPosition = [stepper convertPoint:CGPointZero toView:self.tableview];
        NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:cursorPosition];
    
        custom *currentCell = (custom *)[self.tableview cellForRowAtIndexPath:indexPath];
    
        double value = [stepper value];
    
        [currentCell.lblCount setText:[NSString stringWithFormat:@"%d", (int)value]];
    
        self.arrayCounts[indexPath.row][@"counts"] = [NSString stringWithFormat:@"%d",(int)value];
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    }
    
    
    - (IBAction)placeorder:(id)sender {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"counts != %@",@"0"];
        NSArray *filteredArray = [self.arrayCounts filteredArrayUsingPredicate:predicate];
        NSLog(@"yoyr arry:%@",filteredArray);
    }
    

    enter image description here