我正在解析json以显示tableview中的内容。我有包含解析的json的数组在getReceivedData中填充,在UITAbleView Delegate方法之后调用。所以在填充tableview时遇到问题,因为当编译器尝试填充它时,数组尚未初始化。
- (void)getReceivedData:(NSMutableData *)data sender:(RestAPI *)sender{
NSError * error=nil;
NSArray *receivedData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSString *dictionaryKey=@"department";
NSString *predicateString=@"software";
NSPredicate *predicate=[NSPredicate predicateWithFormat:@" %K == %@ ", dictionaryKey,predicateString];
NSArray *shortlisted=[receivedData filteredArrayUsingPredicate:predicate];
for(int i = 0; i<shortlisted.count; i++)
{
NSDictionary *detailItems=[shortlisted objectAtIndex:i];
NSString *name=[detailItems objectForKey:@"emp_name"];
NSString *designation=[detailItems objectForKey:@"designation"];
NSString *email=[detailItems objectForKey:@"email"];
NSString *phone_no=[detailItems objectForKey:@"phone_no"];
// NSString *image=[detailItems objectForKey:@"url_path"];
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name, @"keyname",
designation, @"keydesignation",
email, @"keyid",
phone_no, @"keyphone",
nil];
[myObject1 addObject:dictionary1];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
if(isfiltered==YES){
return [filteredArray count];
}
else{
return [myObject1 count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"myCell"];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:@"MyTableCell" bundle:nil]
forCellReuseIdentifier:@"myCell"];
cell=[tableView dequeueReusableCellWithIdentifier:@"myCell"];
}
if(isfiltered==NO)
{
NSDictionary * tmpdict= [myObject objectAtIndex:indexPath.row];
cell.nameLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyname"]];
cell.designationLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keydesignation"]];
cell.idLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyid"]];
cell.phoneLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyphone"]];
cell.mainImg.image = [UIImage imageNamed:[tmpdict objectForKeyedSubscript:@"keyimage"]];
}
else{
NSDictionary * tmpdict= [filteredArray objectAtIndex:indexPath.row];
cell.nameLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyname"]];
cell.designationLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keydesignation"]];
cell.idLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyid"]];
cell.phoneLabel.text=[NSMutableString stringWithFormat:@"%@",[tmpdict objectForKeyedSubscript:@"keyphone"]];
cell.mainImg.image = [UIImage imageNamed:[tmpdict objectForKeyedSubscript:@"keyimage"]];
}
return cell;
}
答案 0 :(得分:4)
首先,在一个可变字典中,你必须给对象然后它的键。你做错了
for(int i = 0; i<shortlisted.count; i++)
{
NSDictionary *detailItems=[shortlisted objectAtIndex:i];
NSString *name=[detailItems objectForKey:@"emp_name"];
NSString *designation=[detailItems objectForKey:@"designation"];
NSString *email=[detailItems objectForKey:@"email"];
NSString *phone_no=[detailItems objectForKey:@"phone_no"];
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name,@"keyname",
designation,@"keydesignation",
email,@"keyid",
phone_no,@"keyphone",
nil];
[myObject1 addObject:dictionary1];
}
答案 1 :(得分:0)
实际上这并不是像你一样初始化字典的正确方法,@“%@”使用%@是任何对象的格式字符串中的占位符。
dictionary1=[NSMutableDictionary dictionaryWithObjectsAndKeys:
name,@"keyname",
designation,@"keydesignation",
email,@"keyid",
phone_no,@"keyphone",
nil];