2012-05-25 2 views
0

저는 비용 추적 프로그램과 같은 종류의 응용 프로그램을 만들고 있습니다.섹션 제목을 가져 오는 중 오류가 발생했습니다.

내 요구 사항은 섹션 헤더에서 날짜를 얻고 해당 날짜에 추가 된 비용을 테이블보기에서 얻는 것입니다. 다음 코드를 시도했지만 작동하지 않습니다.

-(IBAction)bydate:(id)sender 
{ 
[self.byDateArray removeAllObjects]; 
[self.byDateCountArray removeAllObjects]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
for(NSManagedObject *info in self.listOfExpenses){ 
    NSString *compareDates = [dateFormatter stringFromDate:[info valueForKey:@"date"]]; 
    BOOL isAvail = NO; 
    for (int i = 0; i<[self.byDateArray count]; i++){ 
     if([compareDates isEqualToString:[self.byDateArray objectAtIndex:i]]) 
     { 
      isAvail = YES; 
     } 
    } 
    if(!isAvail) 
     [self.byDateArray addObject:compareDates]; 
} 
int count = 0; 
for (int i = 0 ; i < [self.byDateArray count] ; i ++){ 
    NSString *compareDates = [self.byDateArray objectAtIndex:i]; 
    for(NSManagedObject *info in self.listOfExpenses){ 
     if([compareDates isEqualToString:[dateFormatter stringFromDate:[info valueForKey:@"date"]]]) 
     { 
      count++; 
     } 
    } 
    [self.byDateCountArray addObject:[NSNumber numberWithInt:count]]; 
    count = 0; 
} 
self.byDateTab.hidden = NO; 
self.byDateTab.frame = CGRectMake(0, 123, 320, 244); 
[self.view addSubview:self.byDateTab]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
if(tableView == self.byDateTab) 
    return [self.byDateArray count 
return 3; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
int rows; 
if(tableView == self.byDateTab) 
    rows = [[self.byDateCountArray objectAtIndex:section] intValue]; 
return rows; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"]autorelease]; 
if (tableView == self.byDateTab) 
{ 
for(int i = 0; i < [self.byDateCountArray count];i++) 
{ 
    if(indexPath.section == 0) 
    { 
     NSManagedObject *records = nil; 
     records = [self.listOfExpenses objectAtIndex:indexPath.row]; 

     self.firstLabel.text = [records valueForKey:@"category"]; 
     self.secondLabel.text = [records valueForKey:@"details"]; 

     NSString *amountString = [NSString stringWithFormat:@"%@",[records valueForKey:@"amount"]]; 
     self.thirdLabel.text = amountString; 
    } 
    else if (indexPath.section == i) 
    { 
     int rowCount = 0; 
     for(int j=0; j<indexPath.section; j++) 
     { 
      rowCount = rowCount + [[self.byDateCountArray objectAtIndex:j]intValue]; 
     } 
     NSManagedObject *records = nil; 
     records = [self.listOfExpenses objectAtIndex:(indexPath.row + rowCount) ]; 

     self.firstLabel.text = [records valueForKey:@"category"]; 
     self.secondLabel.text = [records valueForKey:@"details"]; 

     NSString *amountString = [NSString stringWithFormat:@"%@",[records valueForKey:@"amount"]]; 
     self.thirdLabel.text = amountString; 
    } 
} 
} 

그러나 내가 NSlog

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index 2 beyond bounds for empty array' 
+0

하나의 tableview가 있습니까? – adali

+0

아니요 내가 6 tableviews – ichanduu

+0

이 시점에서 귀하의 배열 byDateArray 비어 있습니다. 에러 로그에서 명확한 것은 경계 범위가 브레이크 포인트를 적용하거나 [self.byDateArray objectAtIndex : section]을 사용하기 전이라고 말합니다. byDateArray 및 섹션의 수를 기록하거나 포인트를 사용하십시오 – superGokuN

답변

0

문제에 다음과 같은 오류를 얻고있다

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
return [self.byDateArray objectAtIndex:section]; 
} 

이 라인에서 SIGABRT 오류를 얻고 여기에

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if(tableView == self.byDateTab) 
     return [self.byDateArray count]; 
    //why crashed at index 2, because here is 3 
    return 3; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [self.byDateArray objectAtIndex:section]; 
} 

입니다 그것은 다른 tabl에 충돌한다. 하지 "self.byDateTab"

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if(tableView == self.byDateTab) 
    { 
     return [self.byDateArray objectAtIndex:section]; 
    } 
    else 
    { 
     //you should deal with other tableview 
     // it crashed here 
    } 
} 

하거나 오류에 따라 다른있는 tableview의 섹션 헤더

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if(tableView == self.byDateTab) 
     return [self.byDateArray count]; 

    //i don't see other tableviews how to work, so if you do this, the code will work 
    //then you should look back to the solution above 
    return 0; 
} 
+0

감사합니다. 그것의 일. – ichanduu

0

을 사용하지 eview 당신의 self.byDateArray 인덱스 2의 물건을 가지고 있지 않기 때문에의 내용을 확인하시기 바랍니다 귀하의 배열.

+0

그 방법을 제거하면 어떤 섹션도없이 레코드를 얻게됩니다. – ichanduu

0

byDateArray은 비어 있습니다.

[self.byDateArray removeAllObjects];으로 무엇을하고 있습니까?

+0

동일한 배열을 사용하고있는 다른 tableViews가 있습니다! .so 파일을 비우는 중입니다. 이유는 무엇입니까? – ichanduu

+0

왜'numberOfSectionsInTableView'에'3'을 반환합니까? '];'도 없습니다. – pre

0

위에서 언급 한 것처럼 문제는 객체를 추가하는 것입니다. self.byDateArray self.byDateArray이 초기화되지 않았습니까? -(IBAction)bydate:(id)sender 방법에서 [self.byDateArray removeAllObjects]; 바로 앞에 NSLog(@"self.byDateArray: %@",self.byDateArray)을 입력하십시오. 이것은

0

section

값보다 큰

self.byDateArray 

카운트 수단.

NSLog(@" %@ ", self.byDateArray); 

에 - (있는 NSString *)있는 tableView :

은 첫 번째 코드 라인이 추가 시도 배열의 내용을 참조하기위한 기능.

관련 문제