【问题标题】:iPhone Array with Dictionary with Array (ListApp)iPhone Array with Dictionary with Array (ListApp)
【发布时间】:2014-07-30 19:55:27
【问题描述】:

我正在开发一个列表应用程序。我有两个数组(动物和汽车)。代码如下:

...

编辑:

这是整个 .m 的新代码以及您的建议: 现在一切都在一个列表中。现在如何将它们分组到列表中?

#import "ListeViewController.h"

@interface ListeViewController ()

@end

@implementation ListeViewController{

    NSArray *kategorien;
    NSArray *autosArray;
    NSArray *tiereArray;
    NSArray *kategorienArray;
    NSArray *combined;
    NSDictionary *allgemein;

}


-(instancetype) initWithCoder:(NSCoder *)aDecoder{

    self = [super initWithCoder:aDecoder];

    if (self){
        self->kategorien = @[@"Autos", @"Tiere"];
        self->autosArray = @[@"Opel", @"Mercedes", @"Audi"];
        self->tiereArray = @[@"Hamster", @"Ente", @"Schwan", @"Pferd", @"Pinguin"];
        self->combined = [autosArray arrayByAddingObjectsFromArray:tiereArray];
        self->allgemein = [NSDictionary dictionaryWithObjects: combined forKeys:combined];


    }

    return self;
}

-(instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        self->kategorien = @[@"Autos", @"Tiere"];
        self->autosArray = @[@"Opel", @"Mercedes", @"Audi"];
        self->tiereArray = @[@"Hamster", @"Ente", @"Schwan", @"Pferd", @"Pinguin"];
        self->combined = [autosArray arrayByAddingObjectsFromArray:tiereArray];
        self->allgemein = [NSDictionary dictionaryWithObjects: combined forKeys:combined];

    }

    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

#pragma mark -
#pragma mark Table view data source

-(NSInteger) numberOfSectionsInTableView:(UITableView*) tableView{

    return 1;
}

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

    return [combined count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *CellIdentifier =@"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    }
                    }
                    //Configure the Cell

                    cell.textLabel.text = [combined objectAtIndex:indexPath.row];

                    return cell;
                    }



#pragma mark -
#pragma mark Table view delegate

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

    NSLog(@"In did Select Row At Index Path");
    NSLog(@"Row %d" ,indexPath.row);

                    }


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

(sry 或者数组和字典的德语标记,也sry 用于一些拼写和语法)

【问题讨论】:

  • 谁教你使用self->xxx?您是否研究过属性以及如何使用它们?为什么需要字典?
  • 我正在小组中开发一个更大的应用程序。和它的培训,但没有人知道如何处理。在更大的应用程序中,我们必须放入广告。并且使用这种类型的字典管理广告。

标签: ios objective-c nsarray nsdictionary


【解决方案1】:

您可以使用NSDictionary's dictionaryWithObjects:forKeys 创建基于两个数组的 NSDictionary。

你会这样做的方式是:

self.allgemein = [NSDictionary dictionaryWithObjects:autosArray forKeys:tiereArray];

如果您想组合两个数组,然后将它们存储在具有相同键和对象的字典中(尽管我不确定您为什么要这样做),然后这样做:

NSArray *combined = [self.autosArray arrayByAddingObjectsFromArray:self.tiereArray];
self.allgemein = [NSDictionary dictionaryWithObjects:combined forKeys:combined];

【讨论】:

  • 好的,谢谢。到这里我才明白。但是如何处理它,当“autosArray”和“tiereArray”都是字典和键的数组时?我现在必须写self.allgemein = [NSDictionary dictionaryWithObjects:autosArray, tiereArray forKeys:autosArray, tiereArray]; 吗?
  • 如果我理解正确,您正在尝试创建一个字典,其中键是tiereArray,对象是autosArray?即:@{@"Hamster" => @"Opel", @"Ente" => @"Mercedes" ... }
  • 如果您尝试创建一个字典,其中键和对象相同并且同时包含 tiereArray 和 autosArray,请查看我编辑的答案。
  • 谢谢。第一个问题解决了。现在是另一个(请看我的编辑)。
  • 按什么分组?您能否编辑您的问题以显示您期望数据的外观?
猜你喜欢
  • 1970-01-01
  • 2011-01-30
  • 2020-07-09
  • 2017-03-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-26
  • 2022-12-27
相关资源
最近更新 更多