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;
    }
}
}

しかし、この行でSIGABRTエラーが発生しています

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

NSlog に次のエラーが表示されます

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

5 に答える 5

0

エラーによると、self.byDateArrayインデックス2にオブジェクトがないため、配列の内容を確認してください。

于 2012-05-25T07:54:55.653 に答える
0

あなたbyDateArrayは空っぽのようです。

何をしているの[self.byDateArray removeAllObjects];

于 2012-05-25T07:55:57.340 に答える
0

上記のように、問題はあなたにオブジェクトを追加することですself.byDateArray たぶんself.byDateArray初期化されていませんか?あなたの方法でNSLog(@"self.byDateArray: %@",self.byDateArray)直前にしてください。[self.byDateArray removeAllObjects];-(IBAction)bydate:(id)sender

于 2012-05-25T08:02:46.243 に答える
0

問題はここにあります

-(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];
}

「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
    }
}

または、他のテーブルビューのセクションヘッダーを無効にするだけです

-(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;
}
于 2012-05-25T07:53:52.817 に答える
0

これは、

section

値がより大きい

self.byDateArray

カウント。

これを最初のコード行に追加してみてください:

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

in -(NSString *)tableView: 配列の中身を見る関数。

于 2012-05-25T08:06:34.340 に答える