Add data from one object to a UITableView
i writed this code ,
NSDictionary *json = [responseString JSONValue];
Status *statut = [[Status alloc] init];
statut.flyNumber = [json objectForKey:@"flynumber"];
statut.ftatuts = [json objectForKey:@"fstatuts"];
statut.escDepart = [json objectForKey:@"escdepart"];
statut.escArrival = [json objectForKey:@"escarrival"];
statut.proArrival = [json objectForKey:@"proarrival"];
statut.proDepart = [json objectForKey:@"prodepart"];
statut.estDepart = [json objectForKey:@"estdepart"];
statut.estArrival = [json objectForKey:@"estarrival"];
statut.realDepart = [json objectForKey:@"realdepart"];
statut.realArrival = [json objectForKey:@"realarrived"];
[dataToDisplay addObject:statut];
[self.tableView reloadData];
and i want to put the result statut object in a table view , each attribute in a line ( cell ) . i don't know how to to . I writed this but they show them in the same line .
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSLog(@"1 ok");
Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];
NSLog(@"2 ok");
cell.textLabel.text = [NSString stringWithFormat:@"%@%@",statut2.flyN开发者_Go百科umber,statut2.ftatuts];
NSLog(@"3 ok");
return cell;
Help please
Maybe you could have your Statut class return an array containing all of the properties you set from your JSON data. Then you can simply use the array to set the number of rows and the cell for each row.
Here is a solution that allows you to configure the order in which your attributes will appear in your tableView:
// declare this enum in your .h file
enum {
flyNumberRow = 0, // first row in tableView
ftatutsRow = 1, // second row
escDepartRow = 2, // etc.
escArrivalRow = 3,
proArrivalRow = 4,
proDepartRow = 5,
estDepartRow = 6,
estArrivalRow = 7,
realDepartRow = 8,
realArrivalRow = 9
}
In your -tableView:cellForRowAtIndexPath:
method implementation:
// ...
// initialize the cell as you did in your code already
Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];
switch(indexPath.row) {
case flyNumberRow:
cell.textLabel.text = [NSString stringWithFormat:@"%@",statut2.flyNumber];
break;
case ftatutsRow:
cell.textLabel.text = [NSString stringWithFormat:@"%@",statut2.ftatuts];
break;
// ... and so on for each case
}
return cell;
Evidently, tableView:numberOfRowsInSection:
must return 10
.
精彩评论