将一个对象的属性放在不同的行中 [英] Put attribute of one object in differents rows
问题描述
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];
但如果我这样做:
Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];
NSLog(@"2 ok");
cell.textLabel.text = [NSString stringWithFormat:@" %@ %@",statut2.escDepart,statut2.escArrival];
NSLog(@"3 ok");
return cell;
所有信息都在一行中。但我想在不同的行中构建我的信息。例如在第一个例子中,我想把statut.escDepart和escArrival放在第二个statut.proDepart和statut.proArrival ....
我试着遵循这个例子,
All information will be in one row .But i want to structured my informations in differents rows .For exemple in the first exemple , i want to put statut.escDepart and escArrival , in the second statut.proDepart and statut.proArrival .... I tried to follow this exemple ,
// 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
}
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
但它不是'工作。我有错误。帮助我pleaaaase
这是错误。
http://hpics.li/b5653ce
But it don't work .I have errors.Help me pleaaaase this is the error . http://hpics.li/b5653ce
推荐答案
您在 dataToDisplay
中添加了 Status
的单个实例。因此,当请求第二行时, dataToDisplay
没有第二个对象,并且您的应用程序正在崩溃。我不认为你应该在这里使用数组。既然您已经知道了单元格的数量,请手动将 numberOfRowsInSection:
返回为10.而不是数组对象,请保持状态
对象。然后在 tableView:cellForRowAtIndexPath:
中,使用switch语句为单元格赋值。从我看到的,你可以摆脱 dataToDisplay
。
You are adding a single instance of Status
in dataToDisplay
. So when the second row is requested, dataToDisplay
has no second object and your application is crashing. I don't think you should be using an array here. Since you know the number of cells already, manually return the numberOfRowsInSection:
as 10. Instead of the array object, hold on to the Status
object. Then in tableView:cellForRowAtIndexPath:
, use the switch statement to assign values to the cell. From what I see, you can get rid of dataToDisplay
.
这篇关于将一个对象的属性放在不同的行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!