Iphone 获取节的标题时出错

Iphone 获取节的标题时出错,iphone,objective-c,ios,uitableview,core-data,Iphone,Objective C,Ios,Uitableview,Core Data,我正在创建一个费用跟踪类应用程序 我的要求是在节标题中获取日期,并在表视图中获取在该日期添加的费用。我尝试了以下代码,但不起作用 -(IBAction)bydate:(id)sender { [self.byDateArray removeAllObjects]; [self.byDateCountArray removeAllObjects]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormat

我正在创建一个费用跟踪类应用程序

我的要求是在节标题中获取日期,并在表视图中获取在该日期添加的费用。我尝试了以下代码,但不起作用

-(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' 
问题就在这里

-(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”的tableview中崩溃

或者只禁用其他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;
}

根据错误,您的
self.byDateArray
在索引2中没有任何对象,因此请检查数组的内容

您的
byDateArray
似乎为空


您如何处理
[self.byDateArray removeAllObjects]

如上所述,问题在于向您添加对象
self.byDateArray
可能
self.byDateArray
未初始化? 请在
[self.byDateArray removeAllObjects]之前制作
NSLog(@“self.byDateArray:%@”,self.byDateArray)
-(iAction)bydate:(id)发送者方法中的code>。

这表示

部分

价值大于

self.byDateArray
伯爵

请尝试在第一行代码中添加以下内容:

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

in-(NSString*)tableView:see a content of array的函数。

如果我删除了该方法,我就可以在没有任何分区的情况下获得记录。我拥有使用相同数组的其他tableView!。我正在清空它。这就是我出错的原因吗?为什么在
numberOfSectionsInTableView
中返回
3
。还有
]丢失。此时,您的数组byDateArray为空。它在错误日志中的清除表示超出边界应用断点或在使用[self.byDateArray objectAtIndex:section]之前;记录byDateArray和部分的计数或使用brek pointi更新我的答案,看看:)问题是您有多个tableview
self.byDateArray
NSLog(@" %@ ", self.byDateArray);