gpt4 book ai didi

ios - ios中的程序流程问题

转载 作者:行者123 更新时间:2023-11-28 18:49:22 25 4
gpt4 key购买 nike

我正在解析一个 json 以在 tableview 中显示内容。我在 getReceivedData 中填充了包含已解析 json 的数组,该数组在 UITAbleView 委托(delegate)方法之后调用。因此,当编译器尝试填充 tableview 时数组尚未初始化,这是填充 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;


}

enter image description here这个窗口应该是查看表格的

最佳答案

在可变字典中,首先你必须给对象然后是它的键。你做错了

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];
}

关于ios - ios中的程序流程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44884654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com