【发布时间】: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 发生了一些非常奇怪的事情。
- 在 loadListLoop 方法中覆盖 NSMutableArray(工作正常并使用 NSLog 检查内容)
- 重新加载表(似乎也可以正常工作)
- NSMutableArray 中的内容将回滚到旧内容
有人知道这个问题吗?
EDIT4:
1.在loadListLoop方法上覆盖NSMutableArray(成功) 2.reload table 和 NSMutableArray 只会在这个方法下为空 3.回滚到我在loadListLoop覆盖数据的数据
【问题讨论】:
-
你为什么用
dispatch_sync()而不是dispatch_async()?tableView是否有可能正在等待主队列完成而没有发生?另外,表格视图真的与_tableViewivar 相关联吗?您是否尝试过强制与@synthesize yourTableViewOutlet = _tableView;关联?尝试将NSLog()right 放在块内的reloadData方法之后。 -
>>尝试把你的NSLog()放在我试过的reloadData之后,日志已经输出了
-
你能看看
_tableView实际上不是nil吗?NSLog(@"%@", _tableView);不应打印(null)。如果您包含更多代码会更好...例如您在哪里定义插座和类的基本内容(常用方法等) -
是的 _tableview 不为零。
-
但是您是否连接了情节提要的插座? Ctrl-拖动?还连接了 Storyboard 中的数据源?
标签: ios objective-c uitableview