在NSURL委托后,UITableView代理没有被调用 [英] UITableView delegates not getting called after NSURL delegates

查看:207
本文介绍了在NSURL委托后,UITableView代理没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这也非常有趣,我的第一次NSLog被调用我有0个对象在数组中,第二次连接后已经做了我有25个对象。那就是应该如何但仍然。表格单元格为空...



我的表格视图代表:

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

-



<$ p (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:@ResultCellowner:self options:nil];
cell = [nib objectAtIndex:0];
}

//检索当前队列对象用于此indexPath.row
匹配* currentMatch = [_matchesArray objectAtIndex:indexPath.row];

//现在设置文本
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;

返回单元格;

}

我的连接代理:

   - (void)retrieveResults {

//创建请求。
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:getMatchesURL]];

//开始显示状态栏中的网络活动指示器
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = YES;

//创建URL连接和触发请求
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-



<$ p $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ [[[[[[[[[[[[[[[[[[[[[[[[[response response response response response response response response response response response response response response response response response response response response NSMutableData alloc] init];
}

-



<$ p连接:(NSURLConnection *)连接didReceiveData:(NSData *)data {
[_responseData appendData:data]; $ p>
}

-



<$ p $ NSCachedURLResponse *) - (NSCachedURLResponse *)连接:(NSURLConnection *)连接
willCacheResponse:(NSCachedURLResponse *)cachedResponse {
//返回nil表示不需要存储缓存的响应对于这个连接
return nil;
}

-

   - (void)connectionDidFinishLoading :( NSURLConnection *)连接{
//请求完成并且已收到数据
//您可以解析实例中的内容变量现在


//停止显示状态栏中的网络活动指示器
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO;

//转换为json
_json = [NSJSONSerialization JSONObjectWithData:_responseData选项:NSJSONReadingMutableLeaves error:nil];


//设置匹配数组
_matchesArray = [[NSMutableArray alloc] init]; (int i = 0; i< _json.count; i ++)

{

//创建匹配对象
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];

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


//将Match对象添加到对象数组
[_matchesArray addObject:myMatch];

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

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

-



<$ p $($)$($)$($)$($)$($)$($)$($)$ >

解决方案

我在添加答案,我发现:



原因 [_ resultTableView reloadData]; 没有工作。我已将其更改为 [self.tableView reloadData]; ,它现在可以像一个魅力。



另外如@Zen所提到的,我将段返回值的数量更改为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 {
}

解决方案

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.

这篇关于在NSURL委托后,UITableView代理没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆