Iphone 将一个对象中的数据添加到UITableView
我写了这个密码Iphone 将一个对象中的数据添加到UITableView,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我写了这个密码 NSDictionary *json = [responseString JSONValue]; Status *statut = [[Status alloc] init]; statut.flyNumber = [json objectForKey:@"flynumber"]; statut.ftatuts = [json objectForKey:@"fstatuts"]; statut.escDepart = [json object
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];
我想把结果statut对象放在一个表视图中,每个属性放在一行(单元格)中。
我不知道该怎么做。我写了这个,但他们在同一行显示
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.flyNumber,statut2.ftatuts];
NSLog(@"3 ok");
return cell;
请帮助也许您可以让Statut类返回一个数组,其中包含您从JSON数据中设置的所有属性。然后,您可以简单地使用数组设置行数和每行的单元格。以下是一个解决方案,允许您配置属性在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
}
在-tableView:cellforrowatinexpath:
方法实现中:
// ...
// 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;
显然,
tableView:numberOfRowsInSection:
必须返回10
是否希望每个状态在一个单元格中,每个属性有多行?或者,您希望statut的每个属性位于不同的单元格中,然后每个statut有单独的组吗?我尝试了2个小时,但我无法完成:/Help请帮助当我想在statut中创建一个将返回NSArray的方法时,我有错误“interface”NSArray“无法通过值返回…”正如@Jamie所指出的,您可以使用NSArray来实现,但我不会将该方法放在您的Status类中,因为这是一个模型类,它会违反MVC。相反,让您的控制器定义NSArray属性并在-viewDidLoad:
方法中初始化它,例如:self.statusAttributes=[NSArray arrayWithObjects:statutt.flyNumber,statutt.ftatuts,/*…*/,nil]代码>。谢谢。如果我找不到如何生成数组,我会尝试它,它看起来不是很干净的方法。你说得对,将数据封装到NSArray中更好。请看我对@Jamie答案的评论。。。