2014-01-16 154 views
0

每次迭代新陣列經過這麼長時間,我還在努力創造我的第一個iOS應用程序,而我又一次面臨着另一個問題=(創建在for循環

基本上,我已經拉JSON來自網絡的數據,我得到嵌套的數組,我沒有任何問題迭代數組中的所有對象(包括父數組),並將它們放在一個UITableView。問題是,我想爲表做一些動態的事情。 。我能夠締造出款款的正確數量爲表爲了使您輕鬆直觀地想象我想做的事情,這裏是JSON數據:

{"filter_group":[{"filter_group_id":"1","name":"Location","filter":[{"filter_id":"2","name":"Central"},{"filter_id":"8","name":"East"},{"filter_id":"1","name":"North"},{"filter_id":"10","name":"Northeast"},{"filter_id":"9","name":"West"}]},{"filter_group_id":"3","name":"Price","filter":[{"filter_id":"7","name":"Free"},{"filter_id":"5","name":"$0 - $50"},{"filter_id":"6","name":"$50 - $100"},{"filter_id":"11","name":"$100 - $150"},{"filter_id":"12","name":"$150 - $200"},{"filter_id":"13","name":"Above $200"}]}]} 

當您複製我給你的任何JSON查看器的凌亂塊,你會看到現在,我有兩個父節點和每個子節點。所以基本上,「位置」和「價格」將分成不同的部分。我已經成功地做到了這一點,但我不知道如何將他們的孩子的姓名放在UITableView的正確部分。

所以我現在的想法是,我迭代數組,並將兒童名稱放入另一個數組(這導致兩個部分的名稱進入1個數組)。我的想法是創建在每個迭代一個新的數組:

for(int i = 0; i < [myArray count]; i++) { 
//throw children of different parents into different array 
    NSMutableArray* newArray[i] = (blah blah blah) 
} 

你可能已經看到了,現在的問題:newArray[i]。基本上,我可以接受的事實是,我正在創建newArray0,newArray1等(我需要我的應用程序是動態的)。我如何在iOS編程中做到這一點?

OR

讀什麼我想上面做的,你對我是怎麼把在各章節中的數據不同的想法後,請賜教。

非常感謝!我非常感謝提供的任何幫助= D

P.S.我的回覆可能很慢,請原諒我

+0

什麼是甚麼'NSArray * newArray [i] = ...'?該代碼創建一個可變長度的原始C指針數組(指向「NSArray」實例)。你甚至無法初始化它,因爲它是被禁止的。你想創建一個數組的數組?如果是這樣,你可以使用'NSMutableArray'並填充它......但這聽起來很平凡。在嘗試做出聰明的事情之前,你需要**學習語言(這意味着你絕對需要*精通C編寫iOS應用程序)。 – 2014-01-16 09:15:27

+0

什麼是使用嵌套數組的問題...只是創建一個新的臨時數組並將該數組作爲對象添加到NSMutableArray – BalaChandra

+0

@ H2CO3我知道這是禁止的。這聽起來微不足道,但迄今爲止我還沒有找到解決方案。我對編程有基本的瞭解,從我的經驗來看,完全學習這門語言毫無意義。你不能申請。學習的唯一方法是做到這一點;) – SilverHood

回答

1

在你的應用程序中,你可以使用來自json對象的數組。我做了樣本項目來說明這個aproche:如果你有同樣的問題

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    NSString *jsonString = @"{\"filter_group\":[{\"filter_group_id\":\"1\",\"name\":\"Location\",\"filter\":[{\"filter_id\":\"2\",\"name\":\"Central\"},{\"filter_id\":\"8\",\"name\":\"East\"},{\"filter_id\":\"1\",\"name\":\"North\"},{\"filter_id\":\"10\",\"name\":\"Northeast\"},{\"filter_id\":\"9\",\"name\":\"West\"}]},{\"filter_group_id\":\"3\",\"name\":\"Price\",\"filter\":[{\"filter_id\":\"7\",\"name\":\"Free\"},{\"filter_id\":\"5\",\"name\":\"$0 - $50\"},{\"filter_id\":\"6\",\"name\":\"$50 - $100\"},{\"filter_id\":\"11\",\"name\":\"$100 - $150\"},{\"filter_id\":\"12\",\"name\":\"$150 - $200\"},{\"filter_id\":\"13\",\"name\":\"Above $200\"}]}]}" ; 
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; 

    NSArray *filter_groups = [result objectForKey:@"filter_group"]; 
    self.data = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]]; 
    self.sectionTitles = [[NSMutableArray alloc] initWithCapacity:[filter_groups count]]; 

    for (NSDictionary *filter_group in filter_groups) { 
     NSArray *filters = [filter_group objectForKey:@"filter"]; 
     [self.data addObject:filters]; 
     [self.sectionTitles addObject:[filter_group objectForKey:@"name"]]; 
    } 
    NSLog(@"%@", self.data); 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    return self.sectionTitles[section]; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return [self.data count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    NSArray *sectionArray = self.data[section]; 
    return [sectionArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testCell"]; 
    } 
    NSArray *sectionArray = self.data[indexPath.section]; 
    NSDictionary *filter = sectionArray[indexPath.row]; 
    cell.textLabel.text = [filter objectForKey:@"name"]; 
    return cell; 
} 

@end 

在頭文件(ViewController.h):

#import <UIKit/UIKit.h> 

@interface ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> 
@property (nonatomic, strong) NSMutableArray *data; 
@property (nonatomic, strong) NSMutableArray *sectionTitles; 
@end 

和ViewController.m文件編碼,隨意問。你可以從https://gist.github.com/MaciejGad/8452791獲取代碼

+0

你的代碼做了我想要的。非常感謝!!! = d – SilverHood

0

在每個循環中創建一個新的臨時數組,並將該新的臨時數組添加到Mutable數組,它與新數組[i]類似,同時檢索NSArray * array = [ mutablearray objectAtIndex:i]`

0

請嘗試下面的代碼。

NSMutableDictionary *responseDict; // assign value to responseDict from your response 
     NSMutableArray *mainArray=[[NSMutableArray alloc] initWithArray:[responseDict objectForKey:@"filter_group"]]; 
     for(int i=0;i<[mainArray count];i++) 
     { 
      if([[[mainArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:@"Price"]) 
      { 
       NSArray *pricechildArray=[[mainArray objectAtIndex:i] objectForKey:@"filter"]; 
      } 
      else if([[[mainArray objectAtIndex:i] objectForKey:@"name"] isEqualToString:@"Location"]) 
      { 
       NSArray *locationchildArray=[[mainArray objectAtIndex:i] objectForKey:@"filter"]; 
      } 
     } 

希望這會幫助你。