2017-12-19 20 views
1

내가 가로 모드에서 테이블에 배열의 값을 표시하려고하지만 나는 다음과 같은 오류를 얻을 수 있으며 응용 프로그램 충돌 : 응용 프로그램을 종료앱 종단 인해 캐치되지 않는 예외에 'NSRangeException'

인해를 uncaught exception 'NSRangeException', 이유 : '* - [__ NSCFString substringToIndex :] : 인덱스 9223372036854775807 out of bounds; string length 10 '**.

- (void)viewWillAppear:(BOOL)animated { 

    [super viewWillAppear:YES]; 
    NSLog(@"Orientation Current: %ld", (long)[[UIApplication sharedApplication] statusBarOrientation]); 

if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) { 
    NSLog(@"Here found"); 
    [self forceToOrientation:UIInterfaceOrientationLandscapeLeft]; 
    } 
} 

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 
    [self.completedSubscriptionsTableView setNeedsLayout]; 
    [self.completedSubscriptionsTableView layoutIfNeeded]; 
} 

- (void)viewWillLayoutSubviews { 
    [super viewWillLayoutSubviews]; 
    [self.completedSubscriptionsTableView setNeedsLayout]; 
    [self.completedSubscriptionsTableView layoutIfNeeded]; 
} 




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { 
    return [self.subscriberArray count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier = @"CellIdentifier"; 
    UITableViewCell *cell; 
    PaymentHistoryCellTableViewCell *paymentHistoryCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    [paymentHistoryCell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    PaymentHistory *currentPayment = [self.subscriberArray objectAtIndex:indexPath.row]; 
    NSLog(@"Current Payment: %@", currentPayment); 
    [paymentHistoryCell.lblTransactionNo setText:[NSString stringWithFormat:@"\t%@", [[self.subscriberArray objectAtIndex:indexPath.row] valueForKey:@"Name"]]]; 
    [paymentHistoryCell.lblAmount setText:[NSString stringWithFormat:@"\t%@", [[self.subscriberArray objectAtIndex:indexPath.row] valueForKey:@"address"]]]; 

    if (indexPath.row % 2 == 0) { 
    [paymentHistoryCell.contentView setBackgroundColor:[UIColor colorWithRed:232/255.0 green:232/255.0 blue:232/255.0 alpha:1.0]]; 
} else { 
    [paymentHistoryCell.contentView setBackgroundColor:[UIColor whiteColor]]; 
} 

    cell = paymentHistoryCell; 
    [cell setNeedsLayout]; 
    [cell layoutIfNeeded]; 
    [cell layoutSubviews]; 
    [cell setNeedsUpdateConstraints]; 
    [cell updateConstraintsIfNeeded]; 
    return cell; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    int space = 0; 
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40.0)]; 
    [headerView setBackgroundColor:[UIColor colorWithRed:210/255.0 green:210/255.0 blue:210/255.0 alpha:1.0]]; 
    CGFloat viewWidth = (self.view.frame.size.width/[self.headerItems count]); 
    NSLog(@"viewWidth: %f", viewWidth); 

for(int i = 0; i < [self.headerItems count]; i++) { 
    UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake((space * i) + (viewWidth * i), headerView.frame.origin.y, viewWidth, headerView.frame.size.height)]; 
    [headerLbl.layer setBorderWidth:1.0]; 
    [headerLbl setTextAlignment:NSTextAlignmentCenter]; 
    [headerLbl setText:[self.headerItems objectAtIndex:i]]; 
    [headerLbl setFont:[UIFont boldSystemFontOfSize:15.0]]; 
    [headerLbl.layer setBorderColor:[[UIColor blackColor] CGColor]]; 
    [headerView addSubview:headerLbl]; 
    } 
    return headerView; 
    } 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 40.0; 
    } 

    #pragma mark - Orientation Methods 
- (BOOL)shouldAutorotate { 
    return YES; 
    } 
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 40.0; 
    } 


- (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskLandscapeLeft; 
    } 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { 
    return UIInterfaceOrientationLandscapeLeft; 
    } 


- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
    [self.navigationController popViewControllerAnimated:YES]; 
    }else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
    [self forceToOrientation:toInterfaceOrientation]; 
    } 
    } 

- (void)forceToOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 

    NSLog(@"Forcing orientation to %ld",(long)toInterfaceOrientation); 
    [[UIDevice currentDevice] setValue: 
    [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] 
          forKey:@"orientation"]; 
    } 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 

    } 

- (void)setCompletedInstallations:(NSMutableArray *)compleInstalltions { 
    self.subscriberArray = [[NSMutableArray alloc]init]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"SubmittedAt" ascending:NO]; 
    self.subscriberArray = [NSMutableArray arrayWithArray:  [compleInstalltions sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]]; 

    NSLog(@" These are the completed subscriptions -> %@",_subscriberArray); 
    [self.completedSubscriptionsTableView reloadData]; 
    }); 
} 
+0

배열을 확인하십시오. 오류에 대한 예 : 배열에 10index가 있지만 11 번째에 액세스하려고했습니다. 그런 사례가 있는지 확인하십시오. –

+0

트리거 된 줄을 알려주는 것이 도움이 될 수 있습니다. – Tommy

+0

배열에 액세스하는 유일한 시간은 numberOfRowsInSection, cellForRowAtIndexPath 및 setCompletedInstallations입니다. – DvixExtract

답변

3

9223372036854775807NSNotFound입니다 -1의 부호 캐스트입니다 :

내 코드입니다. 오류 메시지마다 찾으려는 것을 찾지 못한 검색 결과로 -[NSString substringToIndex:]을 호출하고 있습니다.

이 코드 섹션은 표시되어 있지 않으므로 문자열을 분리 할 수 ​​있다고 생각되는 곳을 찾아 보지만 실제로 할 수 있는지 제대로 확인하지는 않습니다. 디버거가 연결된 상태로 실행 해보면 코드의 어느 행에서 예외가 발생했는지 알려줍니다. 거기에서 거꾸로 작업 할 수 있습니다.

+0

내 코드에 추가하지 않은 두 행이 있습니다. scheduledDate = [schedDate substringToIndex : rangeOfSubstring.location]; and completedDate = [complDate substringToIndex : rangeOfSubstringg.location]; – DvixExtract

+0

'rangeOfSubstring' - NSLog' 나'breakpoint '를 검사하면'rangeOfSubstring.location'은'-1 ', 이것은 당신이 그것을 호출하기 위해 호출 한 메서드가 당신이 찾도록 요청한 것을 찾을 수 없다는 것을 의미합니다. – Tommy

+0

ok 시도해 보겠습니다. – DvixExtract

1

사용하려고하는 색인을 찾을 수 없으므로 충돌이 발생합니다. [NSString substringToIndex :] 메서드에 전달할 값을 확인하십시오