Put attribute of one object in differents rows

徘徊边缘 提交于 2020-01-06 04:47:05

问题


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];

But if i will do like this :

Status *statut2 = [dataToDisplay objectAtIndex:indexPath.row];
    NSLog(@"2 ok");
    cell.textLabel.text =  [NSString stringWithFormat:@" %@  %@",statut2.escDepart,statut2.escArrival];
    NSLog(@"3 ok");

    return cell;

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

But it don't work .I have errors.Help me pleaaaase this is the error . http://hpics.li/b5653ce


回答1:


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.



来源:https://stackoverflow.com/questions/6066309/put-attribute-of-one-object-in-differents-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!