1

I am trying to create an iOS app that gets some data from a web service and displays it in customized cells. So far i could verify via NSLog cmd that the data actually is in the corresponding array. This means the Connection delegates getting called. But unfortunately not the TableView delegates. Or lets say they are getting called but maybe at the wrong place? i also tried to reload the Table view after the connectionDidFinishLoading method but it does not work too. Any idea what is wrong?

It is also very interesting that the first time my NSLog is getting called i have 0 objects in the array and the second time after the connection has been made i have 25 objects. That is how it should be. but still. the table cells are empty...

My Table view delegates:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section{
return _matchesArray.count;
}

-

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   NSLog(@"%lu", (unsigned long)_matchesArray.count);
   return _matchesArray.count;
}

-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"Cell";

ResultCell * cell = (ResultCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"ResultCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

// Retrieve the current team object for use with this indexPath.row
Matches * currentMatch = [_matchesArray objectAtIndex:indexPath.row];

 // now set the text
if ([currentMatch.matchesPlace isEqualToString:@"H"]) {
    cell.labelTeamA.text = currentMatch.matchesTeam;
    cell.labelTeamB.text = currentMatch.matchesOpponent;
}else{
    cell.labelTeamA.text = currentMatch.matchesOpponent;
    cell.labelTeamB.text = currentMatch.matchesTeam;
}

cell.labelResult.text = currentMatch.matchesResult;
cell.labelTime.text = currentMatch.matchesDate;
cell.labelPlace.text = currentMatch.matchesPlace;
cell.labelFvrzId.text = currentMatch.matchesFvrzNr;
cell.labelCategory.text = currentMatch.matchesCategory;

return cell;

}

My Connection delegates:

- (void) retrieveResults{

// Create the request.
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:getMatchesURL]];

// start showing network activity indicator in status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

// Create url connection and fire request
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-

#pragma mark NSURLConnection Delegate Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_responseData = [[NSMutableData alloc] init];
}

-

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}

-

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}

-

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now


// stop showing network activity indicator in status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

//convert to json
_json = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingMutableLeaves error:nil];


//setup matches array
_matchesArray = [[NSMutableArray alloc]init];

for (int i = 0; i < _json.count; i++) {

    // create matches object
    NSString * matchTeam = [[_json objectAtIndex:i]objectForKey:@"fcs"];
    NSString * matchOpponent = [[_json objectAtIndex:i]objectForKey:@"opponent"];
    NSString * matchResult = [[_json objectAtIndex:i]objectForKey:@"resultat"];
    NSString * matchTime = [[_json objectAtIndex:i]objectForKey:@"date"];
    NSString * matchPlace = [[_json objectAtIndex:i]objectForKey:@"place"];
    NSString * matchCategory = [[_json objectAtIndex:i]objectForKey:@"category"];
    NSString * matchId = [[_json objectAtIndex:i]objectForKey:@"fvrzid"];

    Matches * myMatch = [[[Matches alloc] init] initMatch: matchId andMatchesCategory: matchCategory andMatchesDate: matchTime andMatchesTeam: matchTeam andMatchesOpponent: matchOpponent andMatchesPlace: matchPlace andMatchesResult: matchResult];


    // add Match object to object array
    [_matchesArray addObject:myMatch];

}
NSLog(@"%lu", (unsigned long)_matchesArray.count);

[_resultTableView reloadData];
NSLog(@"%lu", (unsigned long)_matchesArray.count);
}

-

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
dom
  • 652
  • 1
  • 16
  • 35
  • Check what is the number of sections being returned, because if the number of sections is zero, none of the rest of datasource methods will be getting called. – Zen Sep 27 '13 at 17:23
  • as i guess you should try to do in cell for row at index path replace you line Matches * currentMatch = (Matches *)[_matchesArray objectAtIndex:indexPath.row]; and just try once by returning 1 in noOfRowAtSection – Anurag Soni Sep 27 '13 at 17:26
  • @Zen the number of sections is at the beginning zero. later once the data has been made up, it returns 25. i found out, that if i switch the tab and go back to that view later - all cells are there. – dom Sep 27 '13 at 18:16
  • 1
    I am confused as you are returning the array's count for both the number of sections and the number of rows in section. – Zen Sep 27 '13 at 18:21
  • @Zen you are right, i made a mistake when returning the number of sections. i changed it to return 1; - thanks for mentioning it – dom Oct 06 '13 at 09:38

1 Answers1

0

I am adding the answer as i found it out:

for any reason [_resultTableView reloadData]; did not work. I have changed it to [self.tableView reloadData]; and it now works like a charm.

In addition as mentioned by @Zen i have changed the number of section return value to 1.

dom
  • 652
  • 1
  • 16
  • 35
  • Are you using ARC? If not,then properties of any class must be called with `self.myProperty` instead of `_myProperty`. – Zen Oct 06 '13 at 10:44