如何使用分段控制操作从API到tableview获取数据?

时间:2015-12-17 11:27:32

标签: ios uitableview uisegmentedcontrol

我有一个视图控制器,我添加了1tableview和分段控件。 在默认情况下,选择segmented = 0并将数据加载到tableview,即使用API​​加载数据。 当我在同一个tableview中选择segmented = 1,2,3,4,5时,如何使用API​​加载数据。 这是我的代码

view DidLoad
 {
 [segment setSelectedSegmentIndex:0];
[segment addTarget:self action:@selector(segmentSelected:) forControlEvents:UIControlEventValueChanged];
 }

-(void)segmentSelected:(id)sender
{
if (segment.selectedSegmentIndex == 0)
{
    NSURLRequest *request0 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"API 1"]];
    connection1 =[[NSURLConnection alloc]initWithRequest:request0 delegate:self];
    [connection1 start];
}
else if (segment.selectedSegmentIndex == 1)
    {
        NSURLRequest *request1 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"API 2"]];
        connection1 = [[NSURLConnection alloc]initWithRequest:request1 delegate:self];
        [connection1 start];

    }
else if (segment.selectedSegmentIndex == 2)
{
    NSURLRequest *request2 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"API 3"]];
    connection1 = [[NSURLConnection alloc]initWithRequest:request2 delegate:self];
    [connection1 start];
}
}

  -(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection==connection1)
{
    NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingAllowFragments error:nil];

    contestArray = [dic valueForKey:@"example1"];

    courseArray = [dic valueForKey:@"example2"];

    contestIdArray = [dic valueForKey:@"example3"];

    [progressHud hide:YES];
    [self.tableViewContest reloadData];
}
}

如何管理我的tableviewnumberofrows?

0 个答案:

没有答案