UITableView部分基于substringToIndex

时间:2014-03-17 03:06:59

标签: ios uitableview

我在查看tableView时遇到问题。所有数据都存在于表格视图中,直到我将我的代码添加到名称部分。

这是我按名称获取数组和分区的代码:

-(void)setupReportsFromJSONArray:(NSData*)dataFromReportsArray{
    BOOL found;
    NSError *error;
   // NSMutableArray *reportsArray = [[NSMutableArray alloc] init];
    NSArray *arrayFromServer = [NSJSONSerialization JSONObjectWithData:dataFromReportsArray options:0 error:&error];

    if(error){
        NSLog(@"error parsing the json data from server with error description - %@", [error localizedDescription]);
    }

    else {
        reportsArray = [[NSMutableArray alloc] init];
        for(NSDictionary *eachReport in arrayFromServer)
        {

            ReportsDataObject *report = [[ReportsDataObject alloc] initWithJSONData:eachReport];

            [reportsArray addObject:report];
            NSString *c = [[eachReport objectForKey:@"title"] substringToIndex:1];
            found = NO;

            for (NSString *str in [self.sections allKeys])
            {
                if ([str isEqualToString:c])
                {
                    found = YES;
                }
            }

            if (!found)
            {
                [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
            }
        }

        }
        NSLog(@"Array Populated");
        NSLog(@"%u reports found",reportsArray.count);
        //Now you have your reportsArray filled up with all your data objects



for (NSDictionary *eachReport in arrayFromServer)
{
    [[self.sections objectForKey:[[eachReport objectForKey:@"title"] substringToIndex:3]] addObject:eachReport];
}

// Sort each section array
for (NSString *key in [self.sections allKeys])
{
    [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]]];
 [reportsTable reloadData];

}

}


-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.sections allKeys] count];
}




- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    {
        return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
    }
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }





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

{


    static NSString *cellIdentifier = @"Cell";



    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }

        //The beauty of this is that you have all your data in one object and grab WHATEVER you like
    //This way in the future you can add another field without doing much.

    if([reportsArray count] == 0){
        cell.textLabel.text = @"no reports to show";
    }
    else{





//        ReportsDataObject *currentReport = [reportsArray objectAtIndex:indexPath.row];
//        cell.textLabel.text = [currentReport reportName];







        // in the future you can grab whatever data you need like this
        //[currentReport buildingName], or [currentReport reportName];
    }


    return(cell);







@end

这会在我的tableView中加载一个空白屏幕。关于我哪里出错的任何想法?我在这个代码的基础上构建了一个教程,我试图改变它以使我受益,因为这些细节来自使用plist的教程。

我已经尝试过多种方式,这是我完成此代码的第一种方式,但未导致应用程序终止。

如果我是NSLog这个代码,哪里是最好的记录地点,这样我才能得到有益的信息?

改变编辑

以下是我根据其他帮助输入的新NSLog请求:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    NSUInteger count = [[self.sections allKeys] count];
    NSLog(@"Number of sections: %d", count);
    return count;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    {
        NSUInteger count = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
        NSLog(@"Number of rows in section: %d", count);
        return count;
    }
}

NSLog输出堆栈:

2014-03-17 15:43:52.189 TESG-iConnect[14502:a0b] Array Populated
2014-03-17 15:43:52.189 TESG-iConnect[14502:a0b] 84 reports found
2014-03-17 15:43:52.190 TESG-iConnect[14502:a0b] Number of sections: 5
2014-03-17 15:43:52.191 TESG-iConnect[14502:a0b] Number of rows in section: 0
2014-03-17 15:43:52.191 TESG-iConnect[14502:a0b] Number of rows in section: 0
2014-03-17 15:43:52.192 TESG-iConnect[14502:a0b] Number of rows in section: 0
2014-03-17 15:43:52.192 TESG-iConnect[14502:a0b] Number of rows in section: 0
2014-03-17 15:43:52.192 TESG-iConnect[14502:a0b] Number of rows in section: 0

0 个答案:

没有答案