【发布时间】:2017-12-19 18:06:06
【问题描述】:
我正在尝试以横向模式在表格上显示数组的值,但是我收到以下错误并导致应用程序崩溃:
由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'* -[__NSCFString substringToIndex:]:索引 9223372036854775807 越界;字符串长度 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];
});
}
【问题讨论】:
-
检查您的数组,错误提示 ex-:您的数组有 10index,但您尝试访问第 11 个。检查您是否有任何此类情况。
-
告诉我们在哪条线上被触发可能会有所帮助。
-
唯一一次访问数组是 numberOfRowsInSection、cellForRowAtIndexPath 和 setCompletedInstallations
-
您显示的代码不应产生错误。你只用
cellForRow“玩”字符串,你用withFormat:,所以不在那里。但显然[[self.subscriberArray objectAtIndex:indexPath.row] valueForKey:@"zzz"]这通常是使用valueForKey:的坏习惯,而您已经获得了PaymentHistory。为什么不做[paymentHistoryCell.lblTransactionNo setText:[currentPayment Name]]或类似的东西(或者你可以使用valueForKey:而不是在自定义单元格上添加“\t”效果? -
不相关,但您是否详细了解
cellForRow中的所有后续调用setNeedsLayoutlayoutIfNeeded、layoutSubviews等正在做什么,或者您的意思是不要伤害,把它们都放进去?在任何单元格中调用时,某些方法可能会很昂贵。
标签: ios objective-c arrays uitableview