Nhlanhla Khumalo
Nhlanhla Khumalo

Reputation: 13

Populate numberOfSectionsInTableView using an NSMutableArray

I'm new to Objective C and I'm trying to specify the numberOfSectionsInTableView in my tableView here is my code.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_dateSectionTitles count];
}

The [_dateSectionTitles count] always returns a zero.

This is how i get _dateSectionTitles

- (void) dictionaryItems {
    NSMutableDictionary* dates = [NSMutableDictionary dictionary];
    _dateSectionTitles = [[NSMutableArray alloc] init];
    for (myObject *cItem in _cashArray) {
        NSString* dateString = [cItem.date stringFormattedWithDayAndFullMonth];
        array = [dates valueForKey: dateString];
        if(!array)
        {
            array = [@[cItem] mutableCopy];
            [dates setObject:array forKey: dateString];
            [_dateSectionTitles addObject:dateString];
        }
        else
        {
            [array addObject: cItem];
        }
    }
}

Upvotes: 0

Views: 46

Answers (2)

Chase
Chase

Reputation: 2304

Based on your code you never instantiate your array variable. you need to have to have this somewhere:

NSMutableArray *array = [[NSMutableArray alloc] init];

Upvotes: 0

BorisE
BorisE

Reputation: 385

From taking a quick look, it looks like array is never declared. Meaning that the else statement of your condition is always executed and nothing is added to dateSectionTitles.

NSMutableArray *array = [dates valueForKey: dateString];

Upvotes: 1

Related Questions