Ios 停止UITableView在方向更改时重新加载数据
当设备的方向发生变化时,我的uitableview会调用reloaddata,因为显示的单元格数量发生变化,就我从文档中所了解的情况而言,在布局子视图中会调用,但这对我来说是有问题的,因为我正在后台执行下载,我不希望某些文件突然出现。有没有办法停止默认行为,让我在需要时手动重新加载 编辑: 我会尽量解释得更好。在tableview的顶部,我有一个名为sync的按钮,该按钮从服务器启动同步请求,该同步请求首先获得一个JSON对象,该对象保存我希望在tableview中显示的信息,但每个uitableview项表示我正在从internet下载的文件 当文件下载时,我在屏幕上有一个活动指示器,只有当文件下载完成后,我才想重新加载表。问题是,当用户更改方向时,UITableview会自动调用reloaddata,因此在下载的文件完成下载之前,单元格会填充json中的信息 代码:Ios 停止UITableView在方向更改时重新加载数据,ios,objective-c,uitableview,reloaddata,layoutsubviews,Ios,Objective C,Uitableview,Reloaddata,Layoutsubviews,当设备的方向发生变化时,我的uitableview会调用reloaddata,因为显示的单元格数量发生变化,就我从文档中所了解的情况而言,在布局子视图中会调用,但这对我来说是有问题的,因为我正在后台执行下载,我不希望某些文件突然出现。有没有办法停止默认行为,让我在需要时手动重新加载 编辑: 我会尽量解释得更好。在tableview的顶部,我有一个名为sync的按钮,该按钮从服务器启动同步请求,该同步请求首先获得一个JSON对象,该对象保存我希望在tableview中显示的信息,但每个uitabl
无需在方向更改时重新加载数据,但如果要重新加载表,请在后台下载完成后尝试调用[myTable reloaddata],并且只有方向已更改时,才可以在方向更改时为此设置bool。更改。我不明白表视图与下载文件有什么关系。你能更好地解释一下这个问题或者展示一些代码吗?反过来说,我不希望它在方向改变时重新加载数据。tableview不会在方向改变时自动重新加载数据。也许你把它设置为DidRotateFrominterface方向。请粘贴您的代码,以便我能更好地帮助您!
@implementation BIDManageFilesViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newDataFinishedDownloading) name:kBIDContentManagerContentDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newDataStartedDownloading:) name:kBIDContentManagerStartedDownloadingContent object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentAlreadyUpToDate) name:kBIDContentUpToDate object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentStartingSync) name:kBIDContentStartingSync object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentEndingSync) name:kBIDContentEndingSync object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(singleDownloadFinished) name:kBIDFinishedDownloadingSingleContent object:nil];
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Sync"
style:UIBarButtonItemStyleDone target:self action:@selector(Sync)];
self.navigationItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Display Mode"
style:UIBarButtonItemStyleDone target:self action:@selector(dismissSelf)];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.title = @"Content Manager";
self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
[self.view addSubview:_navigationBar];
[self.navigationBar pushNavigationItem:self.navigationItem animated:NO];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}
-(void)layoutNavigationBar{
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{
self.navigationBar.frame = CGRectMake(0, self.tableView.contentOffset.y, self.view.frame.size.width, self.topLayoutGuide.length + 44);
}
else
{
self.navigationBar.frame = CGRectMake(0, self.tableView.contentOffset.y, self.view.frame.size.height, self.topLayoutGuide.length + 44);
}
NSLog(@"width: %f", self.view.frame.size.width);
NSLog(@"height: %f", self.view.frame.size.height);
self.tableView.contentInset = UIEdgeInsetsMake(self.navigationBar.frame.size.height, 0, 0, 0);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//no need to call super
[self layoutNavigationBar];
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self layoutNavigationBar];
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self layoutNavigationBar];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
self.navigationController.navigationBarHidden = YES;
}
-(void)newDataStartedDownloading: (NSNotification *)notif
{
self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
_hud.labelText = @"Downloading...";
_hud.detailsLabelText = [NSString stringWithFormat:@"1/%@",[notif.userInfo objectForKey:@"downloadFileNumber"]];
}
-(void)singleDownloadFinished
{
NSString *currentText = _hud.detailsLabelText;
NSArray *subStrings = [currentText componentsSeparatedByString:@"/"];
NSInteger downloadsPlusOne = [[subStrings objectAtIndex:0] integerValue]+1;
NSString *newTextForLabel = [NSString stringWithFormat:@"%d/%@", downloadsPlusOne, [subStrings objectAtIndex:1]];
_hud.detailsLabelText = newTextForLabel;
}
-(void)newDataFinishedDownloading
{
_thereIsNewInfo = TRUE;
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self.tableView reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName:kBIDnewDownloadedContentReadyToBeDispayedNotification object:nil userInfo:nil];
}
-(void)contentAlreadyUpToDate
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sync Alert"
message:@"Files Are Already Up To Date"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
-(void)contentStartingSync
{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Syncing...";
}
-(void)contentEndingSync
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
-(void)Sync
{
[AppDelegate.appContentManager downloadContent];
}
-(void)dismissSelf
{
if (![AppDelegate.appContentManager subsetArrayFromFileArrayWithNonVidContentThatShouldDisplay] && ![AppDelegate.appContentManager subsetArrayFromFileArrayWithVideoContentThatShouldDisplay]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Files to Display"
message:@"Please update or enable content"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
return;
}
else if ([AppDelegate.appContentManager subsetArrayFromFileArrayWithNonVidContentThatShouldDisplay])
{
[self dismissViewControllerAnimated:YES completion:Nil];
}
else
{
[self performSegueWithIdentifier:@"goToVideos" sender:self];
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"goToVideos"])
{
((BIDViewController *)segue.destinationViewController).stackedByManager = TRUE;
}
}
@end