我是iOS编程新手,我有一个关于为标签分配值的问题,以下是我从服务中获得的信息
(
{
EmpName = Peter;
Relation = SouthAfrica;
},
{
EmpName = Smith;
Relation = WestIndies;
},
{
EmpName = Andrew;
Relation = England;
},
{
EmpName = John;
Relation = Australia;
},
{
EmpName = Rahul;
Relation = India;
}
)
上面我有5条记录,我的问题是如何将每个EmpName分配给5个标签,比如peter到Label1,Smith到Label2,Andrew到Label3等等。我只得到第一个empName,下面是我试过的代码。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
jsonArray=nil;
NSLog(@"responseString is%@",responseString);
jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments|NSJSONReadingMutableLeaves error:nil];
profileArray=[jsonArray mutableCopy];
NSLog(@"%lu",(unsigned long)[jsonArray count]);
if ([jsonArray count]==0) {
NSLog(@"responseString is%@",responseString);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Validation" message:@"Error Connecting to server" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
else
{
Label1.text = [[jsonArray objectAtIndex:0]objectForKey:@"EmpName"];
}
}
答案 0 :(得分:2)
在标签上添加标签(例如标签= 1表示label1,标签= 2表示label2 .....标签= 5表示标签5)然后在您的其他部分,您可以获得带有标签的所有标签。
for (int i = 0; i < jsonArray.count; i++) {
UILabel *lbl = (UILabel *)[self.view viewWithTag:i];
lbl.text = [[jsonArray objectAtIndex:i] objectForKey:@"EmpName"];
}
答案 1 :(得分:0)
据知道,服务器响应中只有5名员工,但将来这些可能是100或更少。所以你应该使用UITableView。我希望你对UITableView有所了解。