2013-03-06 16 views
2

我正在創建一個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 

回答

1

如果你正在尋找平行陣列,那麼這是一個例子:

NSArray *[email protected][@"A",@"B",@"C",@"D"]; 
NSArray *[email protected][@"Apple",@"BlackBerry",@"Cognizant",@"Dell"]; 


for (NSInteger i=0; i<alphabets.count; i++) { 
    NSLog(@"%@ for %@",alphabets[i],words[i]); 
} 

這將是清楚的是,兩個陣列被一對一映射關係,並且在循環中,我使用相同的索引來匹配記錄。

+0

我猜想似乎都在旁邊我創建的tableView對方?這是一個好開始,謝謝。從數組中顯示隨機單元的困難過程? – 2013-03-06 11:39:36

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