2012-11-17 18 views
0

我的目標C如何將多個對象添加到陣列 - 目標C/iOS版

這裏是新的節目是我的困境:我在拉從Web JSON文件,我能顯示其中一個元素(currDate)到我的tableView,但現在我想顯示更多。從下面的代碼我我會得到它同時顯示currDate和prevDate這裏需要改變

邏輯:

for (NSDictionary *diction in arrayOfEntry) { 
    NSString *currDate = [diction objectForKey:@"Current Date"]; 
    a = currDate; 
    NSString *prevDate = [diction objectForKey:@"Previous Date"]; 
    b = prevDate;  

    [array addObject:a]; 

    } 
    [[self myTableView]reloadData]; 

我不知道如果我需要改變任何東西,但我安裝它顯示我如何顯示陣列我viewTable:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [array count]; 
} 

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

if(!cell) 
{ 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier]; 
} 

cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
//cell.textLabel.text = [array objectsAtIndexes:indexPath.row]; 
return cell; 
} 
+0

,對於環段實際上是在此: (無效)connectionDidFinishLoading :(NSURLConnection *)連接 { NSDictionary * allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; NSDictionary * feed = [allDataDictionary objectForKey:@「feed」]; NSArray * arrayOfEntry = [feed objectForKey:@「entry」]; .... for ...等等等等等等 – user1832095

回答

0

只需添加另一行:

[array addObject:a]; 
[array addObject:b]; 
+0

是的,這就是我所做的,但當我這樣做時程序爆炸,並且在這行代碼中出現SIGABRT錯誤: cell.textLabel.text = [array objectAtIndex:indexPath.row]; – user1832095

+0

如何在.h文件中聲明數組?你是否宣稱它是一個強大的財產? – rdelmar

+0

不,我還沒有成功Global – user1832095

0

使arrayOfEntry全球。在.h文件中

NSArray *arrayOfEntry; 

numberOfRowsInSection

[arrayOfEntry count] 

tableView: cellForRowAtIndexPath

NSString *currDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:@"Current Date"] 
NSString *prevDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:@"Previous Date"] 
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", currDate, prevDate]; 
+0

我應該怎麼處理我的 - (void)connectionDidFinishLoading:(NSURLConnection *)連接,我在上面粘貼了我的for循環? – user1832095