如何在tableview中按字母顺序存储组数据

时间:2016-02-18 05:25:29

标签: ios json nsmutablearray tableview

hii朋友我是ios的新手。我想将json数组数据存储在特定的字母部分。 这是我的json回复

{
    counts = 0;
    "drycleaning_price" = 10;
    id = 2;
    imagename = "";
    "iron_price" = 16;
    name = Bedsheet;
    "wash_price" = 15;
    "washiron_price" = 14;
},
    {
    counts = 0;
    "drycleaning_price" = 12;
    id = 3;
    imagename = d;
    "iron_price" = 16;
    name = "Coat(Man)";
    "wash_price" = 14;
    "washiron_price" = 13;
},
    {
    counts = 0;
    "drycleaning_price" = 15;
    id = 4;
    imagename = f;
    "iron_price" = 10;
    name = "Jeans(Man)";
    "wash_price" = 12;
    "washiron_price" = 16;
},
    {
    counts = 0;
    "drycleaning_price" = 14;
    id = 3;
    imagename = "";
    "iron_price" = 12;
    name = "Pants(Kid)";
    "wash_price" = 18;
    "washiron_price" = 17;
},
    {
    counts = 0;
    "drycleaning_price" = 15;
    id = 2;
    imagename = s;
    "iron_price" = 10;
    name = "Pants(Man)";
    "wash_price" = 13;
    "washiron_price" = 12;
},
    {
    counts = 0;
    "drycleaning_price" = 12;
    id = 1;
    imagename = "";
    "iron_price" = 32;
    name = "Pillow Cover";
    "wash_price" = 30;
    "washiron_price" = 15;
},
    {
    counts = 0;
    "drycleaning_price" = 10;
    id = 1;
    image = shirt;
    "iron_price" = 15;
    name = "Shirt(Women)";
    "wash_price" = 13;
    "washiron_price" = 12;
},
    {
    counts = 0;
    "drycleaning_price" = 12;
    id = 1;
    imagename = a;
    "iron_price" = 9;
    name = "Shirts(Man)";
    "wash_price" = 10;
    "washiron_price" = 25;
},
    {
    counts = 0;
    "drycleaning_price" = 10;
    id = 3;
    image = sd;
    "iron_price" = 10;
    name = "Shorts(Women)";
    "wash_price" = 18;
    "washiron_price" = 14;
},
    {
    counts = 0;
    "drycleaning_price" = 10;
    id = 2;
    imagename = "";
    "iron_price" = 24;
    name = "Skirt(Kid)";
    "wash_price" = 13;
    "washiron_price" = 14;
},
    {
    counts = 0;
    "drycleaning_price" = 12;
    id = 2;
    image = tops;
    "iron_price" = 10;
    name = "Tops(Women)";
    "wash_price" = 13;
    "washiron_price" = 10;
},
    {
    counts = 0;
    "drycleaning_price" = 12;
    id = 1;
    imagename = "";
    "iron_price" = 16;
    name = "Tshirt(Kid)";
    "wash_price" = 15;
    "washiron_price" = 13;
}

这是我的屏幕截图

enter image description here

当我点击侧边栏字母字符时,时间光标将转到特定部分。但是我的json数据显示在所有部分中。但我想按字母顺序排序数据。比较部分值b比仅存储badsheet值。不是其他值

这是我的代码

- (void)viewDidLoad {
[super viewDidLoad];
[NSThread detachNewThreadSelector:@selector(fetchdata) toTarget:self withObject:nil];
NSLog(@"category type=%@",self.category_type);
animalIndexTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
   arrayCounts = [NSMutableArray array];
sorting = [NSMutableArray array];

}

这里是我的json数据加载代码

-(void)fetchdata

{     NSString * login = [[NSString stringWithFormat:@“http://item.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];

                                   NSArray *tempArray = dic_property[@"Data"];
                                   for (int i=0; i<tempArray.count; i++) {
                                       NSMutableDictionary *dictValues = [NSMutableDictionary dictionaryWithDictionary:tempArray[i]];
                                       [dictValues setValue:@"0" forKey:@"counts"];

                                       [sorting addObject:dictValues];

                                       NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
                                      arrayCounts=[sorting sortedArrayUsingDescriptors:@[sort]];
                                       NSLog(@"test=%@",arrayCounts);
                                   }
                                   NSLog(@"test=%@",arrayCounts);

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

                       }];

}

这是tableview方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 26;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return arrayCounts.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    customcell *cell = (customcell *)[self.tableview dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
        cell = [[[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil] objectAtIndex:0];


        cell.lbl_price.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"washiron_price"]];



    cell.txt_value.text = [NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"counts"]];

    cell.lbl_title.text=[NSString stringWithFormat:@"%@",arrayCounts[indexPath.row][@"name"]];


    [cell.stepper addTarget:self action:@selector(itemsChange:) forControlEvents:UIControlEventValueChanged];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
    return index;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [animalIndexTitles objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return animalIndexTitles;
}

请帮助。我想存储特定的区域商店数据。谢谢你的进步。

0 个答案:

没有答案