2

一般的なニュースフィードを呼び出してランダムなシェアを適用し、低中または高の増減で株価に影響を与える iPhone ゲームを作成しています。これを行うために並列配列を使用することが提案されました。それが最善の方法ですか?そして、その方法についてのチュートリアルへの便利なリンクを誰かが持っていますか?

私は単純な配列の設定をしています。しかし、私はxコードが初めてで、配列からアイテムを呼び出すためにコード化されたものを取得する必要があります。

#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
4

2 に答える 2

1

並列配列を探している場合、これは例です:

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]);
}

2 つの配列が 1 対 1 のマッピングで関連付けられており、ループ中に同じインデックスを使用してレコードを照合したことは明らかです。

于 2013-03-06T11:28:10.880 に答える
0

関係のない質問に答えたところですが、私が構築したプロジェクトでは、TableViewのデータソースとして3つの配列を使用しています。

サンプルプロジェクトのリンクは以下のとおりですが、これがコードの要です: https ://dl.dropbox.com/u/3660978/UniversalTableView.zip

-(void)populateArrays {
    // New literal syntax of interacting with Arrays.
    namesArray = @[@"Homer Simpson",@"Marge Simpson",@"Bart Simpson",@"Lisa Simpson",@"Maggie Simpson",@"Mr. Burns",
                   @"Principal Skinner",@"Krusty the Clown"];

    descriptionsArray = @[@"Homer is a lazy, but also a funny father",
                          @"Marge is smart, but not smart enough to not marry Homer lol",
                          @"Bart is just a smart-alec, but he is really funny too",
                          @"Lisa is the smartest one and the brunt of most jokes",
                          @"Maggie is just the baby of the family",
                          @"Mr. Burns is Homer's boss and evil too",
                          @"Principal Skinner is Bart and Lisa's principal at school",
                          @"Krusty is always clowning around (not really that funny though)"];

    imagesArray =  @[@"homer.jpg",@"marge.jpg",@"bart.jpg",@"lisa.jpg",@"maggie.jpg",@"burns.jpg",
                     @"skinner.jpg",@"krusty.jpg"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [namesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCustomCell";
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    cell.customLabel.text = [namesArray objectAtIndex:indexPath.row];
    cell.customTextView.text = [descriptionsArray objectAtIndex:indexPath.row];
    cell.customImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]];


    return cell;
}
于 2013-03-06T13:01:38.143 に答える