首次加载表视图时,此表视图运行良好但当我尝试使用[_tableView reloadData]
重新加载数据时,列表突然根本无法重新加载。
这里的代码:
-(void)loadListLoop{
AppDelegate* delegate = [[UIApplication sharedApplication] delegate];
shuffleLbl.hidden=false;
[self.view bringSubviewToFront:shuffleLbl];
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSString*playlistID=delegate.gPlaylistID;
NSString*token=[ud stringForKey:@"youtube_token"];
NSString *origin = [NSString stringWithFormat: @"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=%@&key=AIzaSyBeFK_llQHRl7TyXoQxGkLDmIfKGzOPezM&access_token=%@",playlistID,token];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:origin]];
NSData *json = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSArray *array = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil];
_titleList=[array valueForKeyPath:@"items.snippet.title"];
_thumbnailList=[array valueForKeyPath:@"items.snippet.thumbnails.default.url"];
_idList=[array valueForKeyPath:@"items.snippet.resourceId.videoId"];
NSLog(@"%@",_titleList);
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
dispatch_sync(dispatch_get_main_queue(), ^{
[_tableView reloadData];
});
}
方法loadListLoop
是使用从另一个类调用的
PlaylistDetailViewController *playlistDetail = [[PlaylistDetailViewController alloc] init];
[playlistDetail loadList];
看起来loadListLoop
已成功调用,并且[_tableView reloadData];
之前的所有内容也已成功加载。
我把NSLog放在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中可以看出,应用程序至少正在尝试重新加载数据,但似乎根本不起作用。
编辑:首先,包含"-(void)loadListLoop"的视图控制器是容器视图。所以目标视图控制器应该在屏幕上
第2版:我在下方的.h文件中定义了出口
#import <UIKit/UIKit.h>
@interface PlaylistDetailViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>{
//IBOutlet UITableView *tableView;
IBOutlet UIButton *shuffleLbl;
}
-(void)exitLoopVoid;
-(void)loadListLoop;
@property (nonatomic,strong) IBOutlet UITableView *tableView;
@property (nonatomic,retain) NSMutableArray *titleList;
@property (nonatomic,retain) NSMutableArray *authorList;
@property (nonatomic,retain) NSMutableArray *thumbnailList;
@property (nonatomic,retain) NSMutableArray *idList;
-(IBAction)shuffle;
@end
重要
第3版:NSMutableArray似乎发生了一些非常奇怪的事情。
- 在loadListLoop方法中覆盖NSMutableArray(工作良好,并使用NSLog检查内容)
- 重新加载表(似乎也运行良好)
- NSMutableArray内的内容将回滚到旧内容
有人知道这个问题吗?
第4版:
1.在loadListLoop方法中覆盖NSMutableArray(成功)2.只有在该方法下,reload表和NSMutableArray才会为null3.回滚到我覆盖loadListLoop 数据的数据
我看到了几个问题。
首先,您发布的代码是一个方法loadListLoop
。但是您发布的代码调用了一个不同的方法loadList
。两者不相连。
第二,你说:
方法"loadListLoop"是使用从另一个类调用的
PlaylistDetailViewController *playlistDetail =
[[PlaylistDetailViewController alloc] init];
[playlistDetail loadList];
那个代码是错误的。它正在创建一个不在屏幕上的PlaylistDetailViewController的全新实例,并在新创建的视图控制器上调用loadList
方法。
视图控制器还没有机会显示自己,所以它的视图属性将为零。此外,视图控制器在用于填充其表视图的模型结构中可能没有任何数据,因此它没有任何可显示的内容。
此外,如果视图控制器的视图结构是在情节提要中定义的,则不能使用alloc/init创建新的视图控制器实例。
在您尝试调用loadList/loadListLoop时,目标视图控制器是否在屏幕上?你需要解释你的通话顺序。
使用NSNotificationCenter将表从一个类更新到另一个类1.在你必须打电话的地方做这件事。
[[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateTable" object:nil userInfo:userInfo];
2.并将这个用于表视图类。在ViewDidLoad方法中编写。
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(refresh_method)
name:@"UpdateTable"
object:nil];
3.
-(void)refresh_method
{
//reload table here.
}