【发布时间】: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