Ich habe eine UITableView, die ich mit Details aus einem Array von Objekten füllen möchte. Die Tabellenansicht zeigt dasselbe Element in jeder Zeile (die richtige Anzahl an Zeilen!) Ich weiß, dass dies ein einfacher sein muss - aber ich kann nicht sehen, wo ich falsch gelaufen bin:Meine UITableView zeigt das gleiche Element in jeder Zeile
Code-Snippet der Ansicht, die initialisiert die Tabellendaten:
- (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];
}
}
... und der Code, der die Tabelle in der nächsten Ansicht lädt ...
#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;
}
Perfekt! Vielen Dank! Ich dachte, es wäre etwas Einfaches! :-) Ich dachte, wenn ich den Tank außerhalb der Schleife initialisiere, würde ich einfach die Werte ändern und effektiv eine Kopie des Objekts hinzufügen ... Danke nochmal !!! :-) – HillInHarwich