2011-11-21 50 views
0

现在我遇到一个奇怪的情况,我需要你的帮助,并原谅我可怜的英语。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; 
} 
+2

向我们展示1.创建数据的代码,2.返回段的数量(' - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView')。 – Till

+0

究竟哪一行崩溃? – DarkDust

+0

在numberOfSectionsInTableView中,返回2,在numberOfRowsInSection中返回1和0.但是在cellForRowAtIndexPath中,我打印了indexPath.section和indexPath.row,有2和1!真奇怪----所以索引2超越界限[0..1]。 –

回答

1

如果indexPath.section是不正确的值,我敢肯定它是与你在返回值 - (NSInteger的)numberOfRowsInSection :(UITableView *)tableView

首先检查这个,因为如果你的数组大小是2,那么通过numberOfRowsInSection返回的值,indexPath.section不能等于或大于这个值。

+0

在numberOfSectionsInTableView中,返回2,在numberOfRowsInSection中,返回1和0,但在cellForRowAtIndexPath中,我打印了indexPath.section和indexPath.row,有2和1!真奇怪。 –

+0

它是,但它肯定不是来自iOS :)检查我们的可变数组是否在每次调用numberOfSectionsInTableView时都不会改变(如添加一个新元素) – LeGom

0

固定。问题不在这里,这是关于这两个帮派桌子。当我使用[UITableView setContentOffset]到表A时,B也会刷新。在这种情况下,表A将重载数据两次,并且当表的contentOffset为零时,它将只重载一次数据。当表快速重载数据两次时,它崩溃。谢谢大家,我对我的错误感到很抱歉。

相关问题