【问题标题】:Error in getting the title for section获取部分标题时出错
【发布时间】:2012-05-25 07:49:26
【问题描述】:

我正在创建一种费用跟踪器类型的应用程序。

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

-(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' 

【问题讨论】:

  • 此时您的数组 byDateArray 为空。它在错误日志中很清楚,它说越界应用断点或在使用之前 [self.byDateArray objectAtIndex:section];记录 byDateArray 和部分的计数或使用断点
  • 我更新了我的答案,看看 :) 问题是你有多个 tableview

标签: iphone objective-c ios uitableview core-data


【解决方案1】:

问题来了

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 如果我删除该方法,我得到的记录很好,没有任何部分
    【解决方案3】:

    您的byDateArray 似乎是空的。

    [self.byDateArray removeAllObjects]; 在做什么?

    【讨论】:

    • 我有其他 tableViews 我在其中使用相同的数组!所以我正在清空它。所以,这是我收到错误的原因吗?
    • 你为什么在numberOfSectionsInTableView中返回3。还有];不见了。
    【解决方案4】:

    如上所述,问题在于向您添加对象self.byDateArray 也许self.byDateArray 没有初始化? 请在 -(IBAction)bydate:(id)sender 方法中将 NSLog(@"self.byDateArray: %@",self.byDateArray) 放在 [self.byDateArray removeAllObjects]; 之前。

    【讨论】:

      【解决方案5】:

      这意味着一个

      section

      值大于

      self.byDateArray
      

      计数。

      尝试在第一行添加这个:

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

      in -(NSString *)tableView: 用于查看数组内容的函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-28
        • 1970-01-01
        相关资源
        最近更新 更多