I got a weird result in my application. when I clicked the back button, scrollViewDidScroll always calls it, and this causes my application to crash.
The error will not happen if I do not scroll to the bottom (just load the view and then click the back button). but when I scroll to the bottom and then click the back button, it forces the shutdown. there is also no error if I scroll the bottom and then scroll back again to the middle of the table.
here is my code snippet.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Securities Instructions";
currentPage = 1;
isReloading = NO;
totalPages = 1;
isScrollingEnable = YES;
...... bla bla bla....
[self.tableView reloadData];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 90, 0);
}
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
NSLog(@"back button pressed");
isScrollingEnable = NO;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
NSLog(@"scroll view called");
if (isScrollingEnable) {
NSLog(@"scroll view get called: scroll enalbe");
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
float reload_distance = 20;
if(y > h + reload_distance) {
if (isReloading==NO) {
if (totalPages>1) {
if (currentPage<totalPages) {
NSLog(@"scroll view called");
currentPage++;
isReloading = YES;
[self getJsonOnLoad];
}
}
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"index:%d",buttonIndex);
if (buttonIndex==0) {
if (totalPages!=9999) {
[self.navigationController popViewControllerAnimated:YES];
}
}
}
- (void) getTotalPages{
NSArray *detailArray = [self.jsonResult objectForKey:@"detail"];
int detailCount = [detailArray count];
if (detailCount>=25) {
if ([jsonHeader objectForKey:@"totalRows"]) {
totalPages = [[jsonHeader objectForKey:@"totalRows"] floatValue]/25;
}else{
totalPages = 9999;
}
}
}
- (void)canRotate{}
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification{
NSLog(@"orientation change...");
[self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
NSLog(@"orientation:%d",orientation);
if (orientation == UIInterfaceOrientationPortrait)
{
landscape = NO;
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 320, 480);
[self.tableView layoutSubviews];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[self.tableView reloadData];
NSLog(@"portraid");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
landscape = YES;
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 480, 320);
[self.tableView layoutSubviews];
[self.tableView reloadData];
NSLog(@"landscape");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
}
in .h
#import <UIKit/UIKit.h>
@interface SecuritiesInstructionsResultViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSDictionary *jsonHeader;
NSInteger currentPage;
Boolean *isReloading;
NSInteger totalRows;
float totalPages;
BOOL landscape;
BOOL isScrollingEnable;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property NSMutableArray *dataAccount;
@property NSMutableArray *dataCashAmount;
@property NSMutableArray *dataCode;
@property NSMutableArray *dataDate;
@property NSMutableArray *dataExtRef;
@property NSMutableArray *dataInsType;
@property NSMutableArray *dataSecuritiesAmmount;
@property NSMutableArray *dataStatus;
@property NSString *dateFrom;
@property NSString *dateTo;
@property NSDictionary *jsonResult;
@property NSString *selectedAccount;
@property NSString *selectedSecurityCode;
@property NSString *selectedSecuritySecCodeType;
@property NSString *selectedSecurityType;
@property NSString *selectedCurrencyCode;
@property NSString *selectedStatus;
@property NSString *selectedDate;
@property NSString *selectedToDate;
@end
I have forbidden not to call the scrollviewdidscroll method when clicking the "Back" button when implementing viewWillDisappear, but this does not help. application still crash and error message.
- ?