我的应用程序得到了奇怪的结果。当我按下后退按钮时,它总是调用scrollViewDidScroll,这会让我的应用程序崩溃。
如果我没有滚动到底部(只是加载视图然后按下后退按钮),则不会发生错误。 但当我滚动到底部,然后按回按钮它强制退出。 如果我滚动到底部然后再次滚动回到tableview的中间也没有错误。
这是我的代码段。
- (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); //values passed are - top, left, bottom, right
}
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
NSLog(@"back button pressed");
isScrollingEnable = NO;
}
// [super viewWillDisappear:animated];
}
- (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];
//server tidak konsisten,parameter totalpages di menu ini tidak ada (menu lain ada) -_-!
if (detailCount>=25) {
if ([jsonHeader objectForKey:@"totalRows"]) {
totalPages = [[jsonHeader objectForKey:@"totalRows"] floatValue]/25;
}else{
//buat unlimited
totalPages = 9999;
}
}
}
//orientation handling
- (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)
// if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
landscape = NO;
//harus di tambahin ini karena parentnya uiviewcontroller bukan uitableviewcontroller jadi tidak otomatis
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];
//load the portrait view
NSLog(@"portraid");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
// else if (orientation == UIInterfaceOrientationLandscapeLeft)
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
landscape = YES;
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
//harus di tambahin ini karena parentnya uiviewcontroller bukan uitableviewcontroller jadi tidak otomatis
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 480, 320);
[self.tableView layoutSubviews];
[self.tableView reloadData];
//load the landscape view
NSLog(@"landscape");
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[self.navigationController.navigationBar setHidden:NO];
}
}
in .h
#import <UIKit/UIKit.h>
@interface SecuritiesInstructionsResultViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSDictionary *jsonHeader;
//untuk paging
NSInteger currentPage;
Boolean *isReloading;
NSInteger totalRows;
float totalPages;
BOOL landscape;
BOOL isScrollingEnable;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;
//@property NSMutableArray *rowData;
@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
我通过实现viewWillDisappear按下后退按钮时阻止不调用scrollviewdidscroll方法,但它没有帮助。该应用程序仍然崩溃,没有错误消息。
任何人都有这个问题的线索吗?
答案 0 :(得分:13)
将UIScrollView或继承类(UITableView,UICollectionView等)的委托设置为nil
目标-C:
- (void)dealloc {
[scrollView setDelegate:nil];
}
夫特:
deinit {
scrollView.delegate = nil
}