现在我遇到一个奇怪的情况,我需要你的帮助,并原谅我可怜的英语。UITableView崩溃时,它滚动到非顶端和说索引2超出界限[0..1]
当我使用[UITableView reloadData]
时,当顶部填充NSArray的UITableView真正重新加载数据时。但是,如果表不在顶端,则不会重新载入数据。事实上,该应用程序崩溃。奇怪的是,indexPath.section
不是正确的价值,大于实际价值。所以控制台说:index 2 beyond bounds [0..1]
。
有人知道为什么吗?请给我一个手或一个提示。
下面是一些代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (pageCount == 0) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"IPOCalendarTableTitle" object:nil];
return 0;
} else {
NSMutableArray *arrayMonths = [stockArray objectAtIndex:pageIndex];
NSMutableArray *arrayDays = [arrayMonths objectAtIndex:0];
IPOListing *listing = [arrayDays objectAtIndex:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd";
NSDate *date = [formatter dateFromString:listing.date];
NSString *s = nil;
NSArray *arrayMonthEN = [NSArray arrayWithObjects:@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec", nil];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if ([delegate.currentLanguage isEqualToString:@"SC"]) {
formatter.dateFormat = @"yyyy年M月";
s = [formatter stringFromDate:date];
} else if ([delegate.currentLanguage isEqualToString:@"TC"]) {
formatter.dateFormat = @"yyyy年M月";
s = [formatter stringFromDate:date];
} else if ([delegate.currentLanguage isEqualToString:@"EN"]) {
int year = [[listing.date substringWithRange:NSMakeRange(0, 4)] intValue];
int month = [[listing.date substringWithRange:NSMakeRange(5, 2)] intValue];
s = [NSString stringWithFormat:@"%@, %d", [arrayMonthEN objectAtIndex:month - 1], year];
}
[formatter release];
NSArray *array = [NSArray arrayWithObjects:s, [NSNumber numberWithInt:pageCount], [NSNumber numberWithInt:pageIndex], nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"IPOCalendarTableTitle" object:array];
NSLog(@"%d", [arrayMonths count]);
return [arrayMonths count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSMutableArray *arrayMonths = [stockArray objectAtIndex:pageIndex];
NSMutableArray *arrayDays = [arrayMonths objectAtIndex:section];
return [arrayDays count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 36.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"IPOCalendarCellIdentifier";
IPOCalendarCell *cell = (IPOCalendarCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"IPOCalendarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lineLabel.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(@"%d, %d", indexPath.section, indexPath.row);
NSMutableArray *arrayMonths = [self.stockArray objectAtIndex:pageIndex];
NSMutableArray *arrayDays = [arrayMonths objectAtIndex:indexPath.section];
IPOListing *listing = [arrayDays objectAtIndex:indexPath.row];
// configuration the cell...
return cell;
}
向我们展示1.创建数据的代码,2.返回段的数量(' - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView')。 – Till
究竟哪一行崩溃? – DarkDust
在numberOfSectionsInTableView中,返回2,在numberOfRowsInSection中返回1和0.但是在cellForRowAtIndexPath中,我打印了indexPath.section和indexPath.row,有2和1!真奇怪----所以索引2超越界限[0..1]。 –