substitute to switch statement in objective c

后端 未结 1 1904
别那么骄傲
别那么骄傲 2021-01-29 00:42

I am loading a tableview with the contents from web service using json frameworks in asynchronous connection. The data is in the json object form

 {\"id\":1,\"f         


        
相关标签:
1条回答
  • 2021-01-29 01:38

    You are going about this the wrong way. You want to create arrays and index them using indexpath.row. So you ll have only one line of assigning cell.textLabel.text. One solution is: Create an NSArray of objects beforehand containing your @"Name",@"E-Mail" etc. like:

    NSArray *arr1=[NSArray arrayWithObjects:@"Name",@"Email",....,nil];
    

    Then when you get the NSDictionary,store it in an array and enumerate through it like

    NSMutableArray *arr2=[dictionaryData allValues];
    for(id obj in arr2)
    {
        if([obj isKindOfClass:[NSString class]])
            [arr2 addObject:obj];
        else if([obj isKindOfClass:[NSDictionary class]])
        {
            [arr2 addObject:[obj valueForKey:@"Monthly"]];
            [arr2 addObject:[obj valueForKey:@"Annual"]];
        }
    }
    

    Then just use indexpath.row in cell.textLabel.text

    cell.textLabel.text=[NSString stringWithFormat:@"%@ : %@",[arr1 objectAtIndex:indexPath.row],[arr2 objectAtIndex:indexPath.row]];
    

    Of course there might be a better way specific to your case, but this should help you build that.

    0 讨论(0)
提交回复
热议问题