简体   繁体   中英

How To Reload Dictionaries On Tableview By Segment Button Click Using Objective C?

I need to create a single tableview with two button UISegment control . I am getting JSON response two different arrays with two keys . For that two keys I created two buttons . whenever I click the segment button one, Then Schools key values should load on tableview. Whenever I click the second segment button Need to load second Key values colleges should reload data on tableview and I want to get table cell click to get all keys of values.

My Response :

 {
    "Schools" =     {
    };
    "colleges" =     {
        A =         {
            roll_no = 0;
           "name" = "SAMS";
       };
        B =         {
            roll_no = 2;
           "name" = "SANF";        
       };
        C =         {
           roll_no = 3;
           "name" = "UDS";   
        };
   }

My Code :

- (IBAction)Click_Segmentbutton:(id)sender {

    switch (segment_Button.selectedSegmentIndex) {
        case 0:{

            [tableview reloadData];
            NSLog(@"SCHOOLS SELECTED");
            break;}
        case 1:{

            [tableview reloadData];
            NSLog(@"COLLEGES SELECTED");
            break;}
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

     NSIndexPath *indexPath;
    if (indexPath.section == 0)
    {
        return [[savedUrl objectForKey:@"Schools"] count];
    }
    else
    {
        return [[savedUrl objectForKey:@"colleges"] count];
    }
}

You have to change the array count according to the segment Index. So as below.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (segment_Button.selectedSegmentIndex == 0)
    {
        return [[savedUrl objectForKey:@"Schools"] count];
    }
    else
    {
        return [[savedUrl objectForKey:@"colleges"] count];
    }
}

Inside your cellForRowAtIndexPath method also change your datasource as above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM