2

我正在创建一个 iPhone 游戏,它将调用一个通用的 Newsfeed 并应用随机份额,以低中或高增加/减少影响股价。有人建议我使用并行数组来解决这个问题。这是最好的方法吗?有没有人有一个有用的链接来指导如何做到这一点?

我有一个简单的数组设置。但是我是 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
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]);
}

很明显,两个数组通过 1-1 映射关联,并且在循环时,我使用相同的索引来匹配记录。

于 2013-03-06T11:28:10.880 回答
0

我刚刚回答了一个不相关的问题,但是我构建的项目使用三个数组作为 TableView 的数据源。

示例项目链接如下,但这是代码的核心: 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 回答