【问题标题】:_tableView reloadData not working when i called method from another class当我从另一个类调用方法时,_tableView reloadData 不起作用
【发布时间】:2016-01-02 02:50:32
【问题描述】:

第一次加载 tableview 时,此 table view 工作正常 但是当我尝试使用[_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

重要

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

  1. 在 loadListLoop 方法中覆盖 NSMutableArray(工作正常并使用 NSLog 检查内容)
  2. 重新加载表(似乎也可以正常工作)
  3. NSMutableArray 中的内容将回滚到旧内容

有人知道这个问题吗?

EDIT4:

1.在loadListLoop方法上覆盖NSMutableArray(成功) 2.reload table 和 NSMutableArray 只会在这个方法下为空 3.回滚到我在loadListLoop覆盖数据的数据

【问题讨论】:

  • 你为什么用dispatch_sync()而不是dispatch_async()tableView 是否有可能正在等待主队列完成而没有发生?另外,表格视图真的与_tableView ivar 相关联吗?您是否尝试过强制与@synthesize yourTableViewOutlet = _tableView; 关联?尝试将 NSLog()right 放在块内的 reloadData 方法之后。
  • >>尝试把你的NSLog()放在我试过的reloadData之后,日志已经输出了
  • 你能看看_tableView实际上不是nil吗? NSLog(@"%@", _tableView); 不应打印 (null)。如果您包含更多代码会更好...例如您在哪里定义插座和类的基本内容(常用方法等)
  • 是的 _tableview 不为零。
  • 但是您是否连接了情节提要的插座? Ctrl-拖动?还连接了 Storyboard 中的数据源?

标签: ios objective-c uitableview


【解决方案1】:

我发现了几个问题。

首先,您发布的代码是一个方法loadListLoop。但是您发布的代码调用了不同的方法loadList。两者没有联系。

第二,你说:

方法“loadListLoop”从另一个类调用

PlaylistDetailViewController *playlistDetail = 
  [[PlaylistDetailViewController alloc] init];
[playlistDetail loadList];

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 2020-06-20
      相关资源
      最近更新 更多