【问题标题】:UITableView delegates not getting called after NSURL delegatesUITableView 委托在 NSURL 委托后没有被调用
【发布时间】:2013-09-27 16:27:49
【问题描述】:

我正在尝试创建一个 iOS 应用程序,该应用程序从 Web 服务获取一些数据并将其显示在自定义单元格中。到目前为止,我可以通过 NSLog cmd 验证数据实际上在相应的数组中。这意味着 Connection 代表被调用。但不幸的是不是 TableView 代表。或者可以说他们被叫到但可能在错误的地方?我还尝试在 connectionDidFinishLoading 方法之后重新加载表格视图,但它也不起作用。知道有什么问题吗?

有趣的是,第一次调用我的 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 {
}

【问题讨论】:

  • 检查返回的节数是多少,因为如果节数为零,则不会调用其余的数据源方法。
  • 我猜你应该尝试在单元格中为索引路径的行替换你的行 Matches * currentMatch = (Matches *)[_matchesArray objectAtIndex:indexPath.row];只需在 noOfRowAtSection 中返回 1 尝试一次
  • @Zen 节数从零开始。稍后,一旦数据完成,它会返回 25。我发现,如果我切换选项卡并稍后返回该视图 - 所有单元格都在那里。
  • 我很困惑,因为您要返回数组的节数和节中的行数。
  • @Zen 你是对的,我在返回节数时出错了。我将其更改为返回 1; - 感谢提及

标签: ios objective-c uitableview delegates nsurlconnection


【解决方案1】:

我正在添加我发现的答案:

出于任何原因[_resultTableView reloadData]; 不起作用。我已将其更改为 [self.tableView reloadData];,现在它就像一个魅力。

另外,正如@Zen 所提到的,我已将部分返回值的数量更改为 1。

【讨论】:

  • 您在使用 ARC 吗?如果不是,则任何类的属性都必须使用self.myProperty 而不是_myProperty 调用。
猜你喜欢
  • 2016-11-08
  • 1970-01-01
  • 2022-01-22
  • 2010-11-18
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
相关资源
最近更新 更多