我正在创建一个 iPhone 游戏,它将调用通用新闻源并应用随机份额,以低中或高增/减影响股价。有人建议我使用并行数组来解决这个问题。那是最好的方法吗?有没有人获得有关如何操作的教程的有用链接?
我有一个简单的数组设置。但是我是 x-code 的新手,我需要获得一些编码来调用数组中的项目。
#import "NewsFeedViewController.h"
@interface NewsFeedViewController ()
@end
@implementation NewsFeedViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Setup NSArray
items = [[NSArray alloc] initWithObjects:@"Galaxy", @"Redbull", @"Boost", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text = [items objectAtIndex:indexPath.row];
return cell;
}
@end
最佳答案
如果你正在寻找并行数组,那么这是一个例子:
NSArray *alphabets=@[@"A",@"B",@"C",@"D"];
NSArray *words=@[@"Apple",@"BlackBerry",@"Cognizant",@"Dell"];
for (NSInteger i=0; i<alphabets.count; i++) {
NSLog(@"%@ for %@",alphabets[i],words[i]);
}
很明显,两个数组是1-1映射关系,在循环时,我使用相同的索引来匹配记录。
关于iphone - 平行阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245839/