UITableView委托在NSURL委托之后没有被调用
我正在尝试创build一个iOS应用程序,从Web服务获取一些数据并将其显示在自定义单元格中。 到目前为止,我可以通过NSLog cmdvalidation数据实际上是在相应的数组中。 这意味着连接代表被调用。 但不幸的是不是TableView代表。 或者让他们说,他们正在打电话,但也许在错误的地方? 我也尝试在connectionDidFinishLoading方法之后重新加载Table视图,但它不起作用。 任何想法是什么错误?
这也是非常有趣的第一次我的NSLog被调用我有0个对象在数组中,第二次连接后,我有25个对象。 这是应该的。 但仍然。 表单元格是空的…
我的表格视图代表:
- (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; }
我的连接代表:
- (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 { }
当我发现它时,我正在添加答案:
出于任何原因[_resultTableView reloadData];
不工作。 我已经改变了[self.tableView reloadData];
现在它就像一个魅力。
另外正如@Zen所提到的,我把段落返回值的数目改为1。