tableView:titleForHeaderInSection:导致崩溃

时间:2009-11-18 21:44:16

标签: objective-c cocoa-design-patterns

出于某种原因,tableView: titleForHeaderInSection:方法导致应用程序崩溃,但没有提供任何有关原因的详细信息。我知道这是这种方法,因为当我评论它时,表格加载,显然没有标题。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *monthArray = [NSArray arrayWithObjects:@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil];

 NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit;
 NSDate *date = [NSDate date];
 NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

 NSInteger year = [dateComponents year];
 NSInteger month = [dateComponents month];

 currentYear = [NSString stringWithFormat:@"%d", year];
 nextYear = [NSString stringWithFormat:@"%d", year+1]; 

 [dateComponents setMonth:month];

 currentYearMonths = [[NSMutableArray alloc] init];
 nextYearsMonths = [[NSMutableArray alloc] init];

 for(uint i=month-1; i<=11; i++){
  [currentYearMonths addObject:[monthArray objectAtIndex:i]];
 }
 for(uint i=0; i<month-1; i++){
  [nextYearsMonths addObject:[monthArray objectAtIndex:i]];
 }

 [calendar release];
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
 NSString *sectionHeader = nil;

 if(section == 0) {
  sectionHeader = currentYear;
 }
 if(section == 1) {
  sectionHeader = nextYear;
 }  
 return sectionHeader; 
}

3 个答案:

答案 0 :(得分:2)

我猜测在调用委托方法之前发布了currentYearnextYear。尝试retain这些ivars。

答案 1 :(得分:1)

您遗漏了一些保留语句,请在下方设置currentYearnextYear,以了解应该修复的内容。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *monthArray = [NSArray arrayWithObjects:@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil];

    NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit;
    NSDate *date = [NSDate date];
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

    NSInteger year = [dateComponents year];
    NSInteger month = [dateComponents month];

    currentYear = [[NSString stringWithFormat:@"%d", year] retain];
    nextYear = [[NSString stringWithFormat:@"%d", year+1] retain]; 

    [dateComponents setMonth:month];

    currentYearMonths = [[NSMutableArray alloc] init];
    nextYearsMonths = [[NSMutableArray alloc] init];

    for(uint i=month-1; i<=11; i++){
        [currentYearMonths addObject:[monthArray objectAtIndex:i]];
    }

    for(uint i=0; i<month-1; i++){
        [nextYearsMonths addObject:[monthArray objectAtIndex:i]];
    }

    [calendar release];
}

答案 2 :(得分:0)

正如其他人所说,实例变量引用的对象没有被正确保留。

要清楚,您用来获取字符串的“便利”方法会分配新的字符串。他们还将新对象放在自动释放池中。

例如,使用“便利”方法:

currentYear = [NSString stringWithFormat:@"%d", year];

与:

相同
currentYear = [[[NSString alloc] initWithFormat:@"%d", year] autorelease];

由于您想要保留这些字符串对象,您可以使用:

currentYear = [[NSString alloc] initWithFormat:@"%d", year];

但你也可以这样做:

currentYear = [[NSString stringWithFormat:@"%d", year] retain];

<强>更新
另外,请记住,您应该在类的dealloc实现中释放实例变量。