iphone - 平行阵列

标签 iphone ios arrays cocoa

我正在创建一个 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/

相关文章:

iphone - UITextView 输入 View

ios - 如何在 Swift 中设置属性本身可变的属性值

ios - Flutter iOS 电子邮件文本字段内容类型

ios - 更新 NSArray 中特定索引处的值

iphone - openGL ES 2.0 中 ios 的图像处理过滤器

ios - 在 iOS 中创建文件和目录

ios - 当我们使用Objective-C使用手指拖动时xib的垂直滑动

ios - 根据任务结果快速切换 View

ruby - 如何从最后一个元素开始遍历数组? ( ruby )

java - T 是测试用例的数量。N 是数组的大小,k 是数组右旋转的次数。如何在 java 中执行此操作