如何在类别中创建 UITableViewCell 明智地排列 JSON 对象
Posted
技术标签:
【中文标题】如何在类别中创建 UITableViewCell 明智地排列 JSON 对象【英文标题】:How to create UITableViewCell in category wise arrange JSON objects 【发布时间】:2016-07-07 05:23:16 【问题描述】:I want to like this, Please have a look
我有 UITableViewCell 并且我创建了按类别排列的 JSON 数据。我获取了我们所有的价值,但是如何在特定数组中排列类别,例如在两个不同数组下的“business_details”中的第一个类别“汽车”。我想在 UITableViewCell 中安排类别。怎么可能,请帮忙。谢谢你
JSON
"status": [
"business_category_name": "Automotive",
"business_details": [
"img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1467813900-263.jpg",
"name": "ssmpl",
"des": "SIXTH SENSE MARKETING PVT LTD",
"address": "m2",
"email": "hr@sixthsenseit.com",
"phone": "09999999999",
"member_since": "2016-07-06",
"img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1467813900-263.jpg",
"website": "www.sixthsenseit.com",
"city": "indore",
"state": "mp",
"zip": "452001"
,
"img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1467814186-DSC_5854.jpg11.jpg",
"name": "h",
"des": "",
"address": "gkjg",
"email": "jgjg",
"phone": "gjg",
"member_since": "2016-07-06",
"img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1467814186-DSC_5854.jpg11.jpg",
"website": "jg",
"city": "jg",
"state": "gjg",
"zip": "jgj"
]
,
"business_category_name": "Banks\/Credit Unions\/Landers",
"business_details": [
"img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
"name": "PNC Bank",
"des": "PNC offers a wide range of services for all our customers, from individuals and small businesses, to corporations and government entities",
"address": "",
"email": "andrea.kendall@pnc.com",
"phone": "260-422-5922",
"member_since": "2016-05-31",
"img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
"website": "",
"city": "Fort Wayne",
"state": "IN",
"zip": "46808"
]
]
我的代码
- (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];
【问题讨论】:
您可以通过将 business_category 设置为 tableview 的一个部分来做到这一点,并且每个部分的返回 numberOfRows 为 no。 business_details 数组中的对象。 这是可能的,但你的屏幕截图是完美的,比如使用 tableview 标题的编码更多和编码更少。 tableview 标题的高度是固定的。 @Dev.RK 我能做什么请帮忙 我想你想要可扩展的表格视图?表示通过单击部分标题展开/折叠部分。 @BhadreshKathiriya 这个安卓应用的截图,我想在手机应用中实现 【参考方案1】:试试这个-
获取一个全局数组,即 NSMutableArray *arrayForBool;
与您的其他数组。在viewDidLoad()
中分配这个数组,即arrayForBool=[[NSMutableArray alloc]init];
现在在 connectionDidFinishLoading
之前添加这些代码行
[tableview reloadData]
-
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
现在在secondVC.h
文件中-
#import <UIKit/UIKit.h>
@interface secondVC : UIViewController
@property(strong,nonatomic)NSDictionary *myPreviousVCDict;
@end
NSLog()
viewDidLoad()
中的此词典。希望这会帮助你。使用它,您可以制作部分和可扩展部分的自定义标题。
这是输出屏幕-
storyBoard 必须是这个样子-
这就是你想要的 member_details--
【讨论】:
我使用您的代码并运行项目仅显示标题“business_category_name”然后我单击任何一个“business_category_name”并崩溃应用程序和错误显示如下:--[__NSArrayM objectAtIndex:]:索引 6 超出范围 [ 0 .. 5]' 你在吗,我卡住了,请帮忙。 成功运行,但所有类别数据中显示一个类别标题。 Dev.RK 我们所有的工作都很好,点击标题并展开单元格不能明智地划分类别。请帮帮我:( 抱歉回复晚了,但我告诉过你请删除行 cell.name.text=[NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];来自 cellforrowatindexpath 方法 检查新截图..我想你想要这个..我对吗?以上是关于如何在类别中创建 UITableViewCell 明智地排列 JSON 对象的主要内容,如果未能解决你的问题,请参考以下文章
如何在自定义 UITableViewCell 中创建 UIImageVIew? (迅速)
如何在 IOS 中创建具有不同 UITableViewCell 的 TableView?