Ios 如何在按类别排列JSON对象中创建UITableViewCell

Ios 如何在按类别排列JSON对象中创建UITableViewCell,ios,objective-c,iphone,json,uitableview,Ios,Objective C,Iphone,Json,Uitableview,我有UITableViewCell,我创建了按类别排列的JSON数据。我获取了我们所有的值,但是如何在特定数组中安排类别,比如在两个不同数组下的“business_details”中的第一个类别“Automotive”。我想在UITableViewCell中排列类别。怎么可能,请帮忙。多谢各位 JSON 我的代码 -(void)viewDidLoad { [超级视图下载]; 名称=[NSMUTABLEARRY new]; ntm=[NSMUTABLEARRY new]; adrsary=[NSM

我有UITableViewCell,我创建了按类别排列的JSON数据。我获取了我们所有的值,但是如何在特定数组中安排类别,比如在两个不同数组下的“business_details”中的第一个类别“Automotive”。我想在UITableViewCell中排列类别。怎么可能,请帮忙。多谢各位

JSON 我的代码
-(void)viewDidLoad
{
[超级视图下载];
名称=[NSMUTABLEARRY new];
ntm=[NSMUTABLEARRY new];
adrsary=[NSMutableArray new];
phoneary=[NSMutableArray new];
emailary=[NSMutableArray new];
websiteary=[NSMutableArray new];
sensary=[NSMutableArray new];
desary=[NSMutableArray new];
image=[NSMutableArray new];
[self.tabel_view setAllowsMultipleSelection:是];
NSURLRequest*req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@]http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_member.php"]];
响应=[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req委托:self];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
n错误*错误;
NSLog(@“接收数据时出错%@”,错误);
NSMutableDictionary*json=[NSJSONSerialization JSONObjectWithData:响应选项:NSJSONReadingMutableLeaves错误:&错误];
NSLog(@“响应数据%@”,json);
NSArray*状态=[json objectForKey:@“状态”];
用于(NSDictionary*状态为状态){
NSString*business_category=[状态值forkey:@“business_category_name”];
[名称添加对象:业务类别];
NSLog(@“业务详细信息%@”,名称);
对于(NSDictionary*详细信息位于[status objectForKey:@“business_details”])
{
NSString*name=[详细信息值forkey:@“name”];
[ntm addObject:name];
NSLog(@“名称%@”,ntm);
NSString*地址=[详细信息值forkey:@“地址”];
[adrsary addObject:address];
NSLog(@“地址%@”,adrsary);
NSString*phone=[详细信息值forkey:@“phone”];
[phoneary addObject:phone];
NSLog(@“phone%@”,phoneary);
NSString*email=[详细信息值forkey:@“email”];
[电子邮件地址对象:电子邮件];
NSLog(@“email%@”,emailary);
NSString*网站=[details valueForKey:@“网站”];
[网站地址对象:网站];
NSLog(@“网站%@”,网站);
NSString*memsens=[详细信息值forkey:@“成员自”];
[感官对象:memsens];
NSLog(@“mem.sense%@”,sensary);
NSString*des=[详细信息值forkey:@“des”];
[desary addObject:des];
NSLog(@“desc.%@”,desary);
NSString*img=[详细信息值forkey:@“img_url”];
[图像添加对象:img];
NSLog(@“图像%@”,图像);
}
}
对于(inti=0;i试试这个-

将全局数组,即
NSMutableArray*arrayforpool;
与其他数组一起使用。在
viewDidLoad()
中分配此数组,即
arrayforpool=[[NSMutableArray alloc]init];

现在在前面的
connectiondFinishLoading
中添加这些代码行
[tableview重新加载数据]
-

for (int i=0; i<[names count]; i++) {
        [arrayForBool addObject:[NSNumber numberWithBool:NO]];
    }
NSLog()
viewDidLoad()
中使用此词汇。希望这对您有所帮助。使用此功能,您可以自定义节头和可扩展节

这是输出屏幕-

故事板必须是这样的-

这是您想要的详细信息--


您可以通过将business_类别设置为tableview的一部分,并返回numberOfRows(每个部分的行数为business_details数组中的对象数)来实现这一点。这是可能的,但您的屏幕截图非常完美,即使用tableview标题进行更多编码和更少编码。tableview标题的高度是固定的。@Dev.RK我能做些什么,请提供帮助nk您想要可扩展的tableview?是指单击节头展开/折叠节。@BhadreshKathiriya这是android应用程序的屏幕截图,我想在I-phone应用程序中实现我使用了您的代码并运行项目仅显示标题“business\u category\u name”,然后单击任意一个“business\u category\u name”崩溃应用程序和错误显示如下:--[\u NSArrayM objectAtIndex:]:索引6越界[0..5]'你在吗,我卡住了,请帮助。成功运行,但在所有类别数据显示中只有一个类别标题。Dev.RK所有工作正常,单击标题并展开单元格只能按类别划分。请帮助我:(很抱歉回复得太晚,但我告诉过你请删除行单元格.name.text=[NSString stringWithFormat:@“%@”,[names objectAtIndex:indexPath.row]];从cellforrowatindexpath方法检查新屏幕截图..我想你想要这个..我说的对吗?
- (void)viewDidLoad 
{
    [super viewDidLoad];
    names = [NSMutableArray new];
    ntm= [NSMutableArray new];
    adrsary=[NSMutableArray new];
    phoneary=[NSMutableArray new];
    emailary=[NSMutableArray new];
    websiteary=[NSMutableArray new];
    sensary=[NSMutableArray new];
    desary=[NSMutableArray new];
    image=[NSMutableArray new];

    [self.tabel_view setAllowsMultipleSelection:YES];

    NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_member.php"]];
    response =[[NSMutableData alloc]init];
    [NSURLConnection connectionWithRequest:req delegate:self];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error;

    NSLog(@"Error in receiving data %@",error);
    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];
    NSLog(@"response data %@",json);

    NSArray *statuses = [json objectForKey:@"status"];

    for(NSDictionary *status in statuses){
        NSString *bussiness_category=[status valueForKey:@"business_category_name"];
        [names addObject:bussiness_category];
         NSLog(@"busness details %@",names);

        for(NSDictionary *details in [status objectForKey:@"business_details"])
        {
            NSString *name=[details valueForKey:@"name"];
            [ntm addObject:name];
            NSLog(@"name %@",ntm);


            NSString *address=[details valueForKey:@"address"];
            [adrsary addObject:address];
            NSLog(@"address %@",adrsary);

            NSString *phone=[details valueForKey:@"phone"];
            [phoneary addObject:phone];
            NSLog(@"phone %@",phoneary);

            NSString *email=[details valueForKey:@"email"];
            [emailary addObject:email];
            NSLog(@"email %@",emailary);

            NSString *website=[details valueForKey:@"website"];
            [websiteary addObject:website];
            NSLog(@"website %@",websiteary);

            NSString *memsens=[details valueForKey:@"member_since"];
            [sensary addObject:memsens];
            NSLog(@"mem. sense %@",sensary);

            NSString *des=[details valueForKey:@"des"];
            [desary addObject:des];
            NSLog(@"desc. %@",desary);

            NSString *img=[details valueForKey:@"img_url"];
            [image addObject:img];
            NSLog(@"image. %@",image);
        }
      }

for (int i=0; i<[names count]; i++) {
    [arrayForBool addObject:[NSNumber numberWithBool:NO]];
}
    [self.tabel_view reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 return names.count;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([[arrayForBool objectAtIndex:section] boolValue]) {
    return emailary.count;
}
else
    return 0;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 70;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

UIView *sectionHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,70)];

sectionHeaderView.backgroundColor=[UIColor grayColor];

sectionHeaderView.tag=section;

UIView *sectionsubHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,60)];

sectionsubHeaderView.backgroundColor=[UIColor blueColor];

UIImageView *arrow=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,60, 60)];

[arrow setImage:[UIImage imageNamed:@"arrow"]];

UILabel *Lbl=[[UILabel alloc]initWithFrame:CGRectMake(60, 0,tableView.frame.size.width-60, 60)];

Lbl.text=[names objectAtIndex:section];

Lbl.textColor=[UIColor whiteColor];

[sectionsubHeaderView addSubview:arrow];

[sectionsubHeaderView addSubview:Lbl];

[sectionHeaderView addSubview:sectionsubHeaderView];

UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];

[sectionHeaderView addGestureRecognizer:headerTapped];

return  sectionHeaderView;

 }

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
 {

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0) {
    BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
    for (int i=0; i<[names count]; i++) {
        if (indexPath.section==i) {
            [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
        }
    }
    [self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];

}


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ht"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }


     cell.title.text=[NSString stringWithFormat:@"%@",[ntm objectAtIndex:indexPath.row]];

    cell.email.text=[NSString stringWithFormat:@"%@",[emailary objectAtIndex:indexPath.row]];
    cell.address_lbl.text=[NSString stringWithFormat:@"%@",[adrsary objectAtIndex:indexPath.row]];
    cell.phone_lbl.text=[NSString stringWithFormat:@"%@",[phoneary objectAtIndex:indexPath.row]];
    cell.web_lbl.text=[NSString stringWithFormat:@"%@",[websiteary objectAtIndex:indexPath.row]];
    cell.sens_lbl.text=[NSString stringWithFormat:@"%@",[sensary objectAtIndex:indexPath.row]];
    cell.des_lbl.text=[NSString stringWithFormat:@"%@",[desary objectAtIndex:indexPath.row]];

    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [image objectAtIndex:indexPath.row]]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    cell.image_view.image =image;

    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

[self.tabel_view reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
for (int i=0; i<[names count]; i++) {
        [arrayForBool addObject:[NSNumber numberWithBool:NO]];
    }
#import "ViewController.h"
#import "MemberTableViewCell.h"
#import "secondVC.h"

@interface ViewController ()
{

    NSMutableData *response;
    NSMutableArray  *arrayForBool;
    NSMutableArray *business_details_array;
    NSMutableArray *names;
    NSDictionary *passDict;


}
@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    names = [NSMutableArray new];

    arrayForBool=[[NSMutableArray alloc]init];

    [self.tableView setAllowsMultipleSelection:YES];

    NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_member.php"]];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    if( connection )
    {

        response = [NSMutableData new];

    }


}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response1
{
    [response setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [response appendData:data];
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];

    NSArray *statuses = [json objectForKey:@"status"];
    names=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:@"business_category_name"]];
    business_details_array=[[NSMutableArray alloc]initWithArray:[statuses valueForKey:@"business_details"]];



    for (int i=0; i<[names count]; i++) {
        [arrayForBool addObject:[NSNumber numberWithBool:NO]];
    }

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return names.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([[arrayForBool objectAtIndex:section] boolValue]) {
        return [[business_details_array objectAtIndex:section] count];
    }
    else
        return 0;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 70;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *sectionHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,70)];

    sectionHeaderView.backgroundColor=[UIColor grayColor];

    sectionHeaderView.tag=section;

    UIView *sectionsubHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,60)];

    sectionsubHeaderView.backgroundColor=[UIColor blueColor];

    UIImageView *arrow=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,60, 60)];

    [arrow setImage:[UIImage imageNamed:@"arrow.png"]];

    UILabel *Lbl=[[UILabel alloc]initWithFrame:CGRectMake(60, 0,tableView.frame.size.width-60, 60)];

    Lbl.text=[names objectAtIndex:section];

    Lbl.textColor=[UIColor whiteColor];

    [sectionsubHeaderView addSubview:arrow];

    [sectionsubHeaderView addSubview:Lbl];

    [sectionHeaderView addSubview:sectionsubHeaderView];

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];

    [sectionHeaderView addGestureRecognizer:headerTapped];

    return  sectionHeaderView;

}

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
    if (indexPath.row == 0) {
        BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
        for (int i=0; i<[names count]; i++) {
            if (indexPath.section==i) {
                [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
            }
        }
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];

    }


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ht"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }


    cell.title.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"name"] objectAtIndex:indexPath.row]];

    cell.email.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"email"] objectAtIndex:indexPath.row]];

    cell.address_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"address"] objectAtIndex:indexPath.row]];

    cell.phone_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"phone"] objectAtIndex:indexPath.row]];

    cell.web_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"website"] objectAtIndex:indexPath.row]];

    cell.sens_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"member_since"] objectAtIndex:indexPath.row]];

    cell.des_lbl.text=[NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"des"] objectAtIndex:indexPath.row]];

    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [NSString stringWithFormat:@"%@",[[[business_details_array objectAtIndex:indexPath.section] valueForKey:@"img_url"] objectAtIndex:indexPath.row]]]];

    UIImage* image = [[UIImage alloc] initWithData:imageData];

    cell.image_view.image =image;

    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];

    passDict=[[business_details_array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    [self performSegueWithIdentifier:@"gotoSeconVC" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.identifier isEqualToString:@"gotoSeconVC"]) {
        secondVC *vc=[segue destinationViewController];
        vc.myPreviousVCDict=passDict;
    }
}
@end
#import <UIKit/UIKit.h>

@interface secondVC : UIViewController
@property(strong,nonatomic)NSDictionary *myPreviousVCDict;
@end