Ios 如何在tableview中按字母顺序存储组数据
hii friends我是ios开发者的新手。我想在特定的字母部分存储json数组数据。 这是我的json响应Ios 如何在tableview中按字母顺序存储组数据,ios,json,nsmutablearray,tableview,Ios,Json,Nsmutablearray,Tableview,hii friends我是ios开发者的新手。我想在特定的字母部分存储json数组数据。 这是我的json响应 { counts = 0; "drycleaning_price" = 10; id = 2; imagename = ""; "iron_price" = 16; name = Bedsheet; "wash_price" = 15; "washiron_price" = 14; }, { counts
{
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;
}
这是我的屏幕截图
当我单击边栏字母时,时间光标将指向特定的部分。但我的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:@”“]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]);
}
});
}];
请帮助。我想存储特定的分区存储数据。感谢您的帮助。检查此…@EICaptain我将参考此链接。但json数据中的问题显示在所有分区中
- (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;
}