TableView load rows from Web Service
I try to load rows from my web service into tableview object in iPhone project with xCode.
I use viewDidLoad method in order to load the items, in this method I call to my web service with this code:
eenAccesoCupon* Servicio = [[eenAccesoCupon alloc] init];
Servicio.logging=NO;
[Servicio GetCuponesEntrantes:self action:@selector(GetCuponesEntrantesHandler:) UsuarioActivo: 4];
I have a NSMutableArray (called listOfItems) that I use to load rows to TableView. If I add the items to this array into GetCuponesEntrantesHandler the tableview doesn't show any row. The sourceCode of this handler is the following:
(void) GetCuponesEntrantesHandler: (id) value {
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(@"%@", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(@"%@", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray* listOfItems = (NSMutableArray*)value;
}
It seems that TableView rows are loaded before web method is called, therefore, tableview methods like numberOfRowsInSection and cellForRowAtIndexPath are called before the web services is invoked.If I load the items of rows in viewDidLoad like this way:
(void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
//Add items
[listOfItems addObject:@"Iceland"];
[listOfItems addObject:@"Greenland"];
[listOfItems addObject:@"Switzerland"];
[listOfItems addObject:@"Norway"];
[listOfItems addObject:@"New Zealand"];
[listOfItems addObject:@"Holland"];
[listOfItems addObject:@"Ireland"];
//Set the title
self.navigationItem.title = @"Countries";
eenAccesoCupon* Servicio = [[eenAccesoCupon alloc] init];
Servicio.logging=NO;
[Servicio GetCuponesEntrantes:self action:@selector(GetCuponesEntrantesHandler:) UsuarioActivo: 4];
}
In this case, tableview object shows only this 7 val开发者_StackOverflow社区ues load in listOfItems, never load any item from GetCuponesEntrantesHandler.
Somebody knows what is the problem?
I suppose that GetCuponesEntrantes: method works with the network asynchronously. This means that it will call the handler once the table view has already been loaded and displayed. To force the table view to reload simply call:
[tableView reloadData]
at the end of the handler method, immediately after
NSMutableArray* listOfItems = (NSMutableArray*)value;assignment.
精彩评论