关于获取NSDictionary的key问题

由于很久没用过 NSDictionary来存放数据,竟然忘了它有 如下两个方法了。

+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

最近一直用它来存放数据model,直接根据它的key来取它的value用,(_dic[@"key_name"])我前几天还在想怎么取到字典的key值,然后做相关的操作,由于没有及时查阅文档,也就把这事忘了,现在突然看到

- (NSArray *)allKeys;

- (NSArray *)allValues;

这两个方法,心里豁然开朗。


关于allkeys和allvalues的一个简单应用,用以加深对这两个属性的理解。

一个简单的UITableView 的显示,section 标题为 老师、学生 row的内容对应为老师1、老师2;学生1、学生2



- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    [selfloadDataFromNetwork];

    

    UITableView * tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,0, 320,460) style:UITableViewStylePlain];

    tableView.delegate =self;

    tableView.dataSource =self;

    [self.viewaddSubview:tableView];

}



- (void)loadDataFromNetwork

{

    //ios6.0 新属性,到时候用用http://www.2cto.com/kf/201303/195697.html

    _dataDic = [[NSMutableDictionaryalloc] init];

   NSArray * arr = [NSArrayarrayWithObjects:@"学生",@"老师",@"工人",@"农民",nil];

   for (int i =0; i < 4; i++) {

        NSMutableArray * arr1 = [[NSMutableArrayalloc] init];

       for (int j =0; j < 5; j++) {

           NSString * str = [NSStringstringWithFormat:@"%@%d",[arrobjectAtIndex:i],j];

            [arr1addObject:str];

        }

        [_dataDicsetObject:arr1 forKey:[arrobjectAtIndex:i]];

    }


}


#pragma mark -

#pragma mark UITableViewDataSource

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

{

    //通过section取得对应段的数组数组有多少项就该有多少行

   NSArray * arr = [_dataDic.allValuesobjectAtIndex:section];

   return arr.count;

}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString * cellID =@"cellID";

   UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

   if (cell == nil) {

        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID] autorelease];

    }

    

    // 获取对应section的所有数据

   NSArray * arr = [_dataDic.allValuesobjectAtIndex:indexPath.section];

    //取得数组的每一项赋值

    cell.textLabel.text = [arrobjectAtIndex:indexPath.row];


   return cell;

}


- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView

{

    //字典当中有多少个就返回多少段

    return_dataDic.count;

}


- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    // 根据section 获取对应段落的标题

   return [_dataDic.allKeysobjectAtIndex:section];

}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    //获取点中行的字符串

   NSString * str = [[_dataDicobjectForKey:[_dataDic.allKeysobjectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];

    

    ChatViewController * ctl = [[ChatViewControlleralloc] init];

    ctl.text = str;

    [selfpresentViewController:ctl animated:YEScompletion:nil];

}


@end



但要记住: _dataDic.allValues    _dataDic.allKeys 得到的数组 是无序的






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值