如何通过分段按钮在Tableview上重新加载字典单击使用目标C? [英] How To Reload Dictionaries On Tableview By Segment Button Click Using Objective C?

查看:90
本文介绍了如何通过分段按钮在Tableview上重新加载字典单击使用目标C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用两个按钮 UISegment控件创建一个 tableview 。我得到 JSON 响应两个不同的数组,带有两个 。对于这两个键,我创建了两个按钮。每当我点击一个段按钮,然后学校键值应加载在tableview上。每当我点击第二个段按钮需要加载第二个键值学院应重新加载tableview上的数据,我想得到表 cell 点击获取所有值的键。

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.

我的回复:

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

我的代码:

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

cellForRowAtIndexPath 方法也会像上面那样更改数据源。

Inside your cellForRowAtIndexPath method also change your datasource as above.

这篇关于如何通过分段按钮在Tableview上重新加载字典单击使用目标C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆