2012-03-26 12 views
0

私はオブジェクトの配列の詳細を設定したいUITableViewを持っています。テーブルビューはすべての行に同じ項目を表示します(正しい行数です!)これは簡単なものでなければならないことはわかっていますが、私がどこに間違っているのかわかりません:私のUITableViewはすべての行で同じアイテムを表示します

ビューを初期化するコードスニペットテーブルデータ:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

{ 

    if([segue.identifier isEqualToString:@"Show Tank List"]) 

    { 

     NSURL *myUrl = [[NSURL alloc]initWithString:@"http://localhost/~stephen-hill9/index.php"]; 
     NSData *data = [[NSData alloc] initWithContentsOfURL:myUrl]; 
     NSError *error; 
     NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
     int i; 
     NSMutableArray *tanksList; 
     tank *thisTank = [[tank alloc] init]; 
     tanksList = [[NSMutableArray alloc] init]; 
     for (i=0; i<json.count; i++) { 
      NSDictionary *bodyDictionary = [json objectAtIndex:i]; 
      thisTank.tankNumber = [bodyDictionary objectForKey:@"ID"]; 
      thisTank.tankProduct = [bodyDictionary objectForKey:@"Product_Desc"]; 
      thisTank.tankPumpableVolume = [bodyDictionary objectForKey:@"Pumpable"]; 
      [tanksList addObject:thisTank]; 
     } 
     [segue.destinationViewController setTanks:tanksList]; 
    } 
} 

...そして次のビューでテーブルをロードするコード...

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1;//keep this section in case we do need to add sections in the future. 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Tank List Table Cell"; 
    UITableViewCell *cell = [self.tankTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) 
    { 
     cell = [[UITableViewCell alloc] initWithFrame:CGRectZero]; 
    } 
    tank *thisTank = [self.tanks objectAtIndex:indexPath.row]; 
    cell.textLabel.text = thisTank.tankNumber; 
    return cell; 
} 

答えて

3

移動この:

tank *thisTank = [[tank alloc] init]; 

forループの内側。同じオブジェクトを何度も何度も更新しています。

また、あなたは細胞間違って初期化している - そうでなければ、新しい細胞のすべての時間を作成し、指定された初期化子を使用し、中に再利用識別子を渡す:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

をそして、あなたは本当にobjective-従ってくださいc命名規則。クラスは大文字で始まり、その他はすべて小文字で始まります。とにかく他の人のために、コードを読みやすくします。

+0

パーフェクト!ありがとうございました!私はそれが何か簡単だろうと思った! :-)私は、ループの外でタンクを初期化することによって、値を変更し、効果的にオブジェクトのコピーを追加すると思った。もう一度ありがとう!!! :-) – HillInHarwich

0

毎回テーブルをリロードしてください!

[self.tableView reloadData];

関連する問題