Json 带有步进器和标签的表视图中的问题

Json issue in tableview with stepper and lable

我是 ios 开发人员的新手。

我想将选定的数据放入其他表格视图,我的问题是我不知道如何获取名称和数量。

看这里是我的截图

我的json数据是这样的格式

 {
        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;
            }
}

这是我的代码

ViewController.h

进口

@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

我卡了三天请解决我的问题 提前致谢。

@Arpit 用 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);
}

您按照错误的方式执行此操作。只需使用在所有索引上具有初始值 0 的 array 并将数组值设置为 cellForRowAtIndexPath 中的标签,而不是在单击 stepper 事件时更新数组索引值,而不是重新加载 table 更新 array.