当我从另一个类调用方法时,_tableView reloadData无法正常工作

时间:2016-01-02 02:50:33

标签: ios objective-c uitableview

首次加载tableview时,此表视图正常工作 但是当我尝试使用[_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里面 看看app是否至少试图重新加载数据,但它似乎根本不起作用。

编辑: 首先,包含“ - (void)loadListLoop”的视图控制器是容器视图。目标视图控制器应该在屏幕上

EDIT2: 我在下面的.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

重要

EDIT3: 看起来NSMutableArray发生了一些非常奇怪的事情。

  1. 在loadListLoop方法中覆盖NSMutableArray(使用NSLog正常工作并检查内容)
  2. 重新加载表(似乎这也正常工作)
  3. NSMutableArray内的内容将回滚到旧内容
  4. 有人对这个问题有所了解吗?

    EDIT4:

    1.在loadListLoop方法中覆盖NSMutableArray(成功) 2.reload table和NSMutableArray仅在此方法时为null 3.回滚到我在loadListLoop上覆盖数据的数据

2 个答案:

答案 0 :(得分:1)

我看到了一些问题。

首先,您发布的代码是方法loadListLoop。但是您发布的代码调用的是另一种方法loadList。两者没有连接。

其次,你说:

使用

从另一个类调用“loadListLoop”方法
PlaylistDetailViewController *playlistDetail = 
  [[PlaylistDetailViewController alloc] init];
[playlistDetail loadList];

那段代码非常错误。它正在创建一个不在屏幕上的全新PlaylistDetailViewController实例,并在新创建的视图控制器上调用loadList方法。

视图控制器还没有机会显示自己,所以它的视图属性将为零。另外,视图控制器可能在其用于填充表视图的模型结构中没有任何数据,因此它不会显示任何内容。

此外,如果视图控制器的视图结构是在故事板中定义的,则不能使用alloc / init来创建新的视图控制器实例。

在您尝试调用loadList / loadListLoop时,屏幕上是否是目标视图控制器?你需要解释你的呼叫顺序。

答案 1 :(得分:1)

使用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.   
}