SwingerDinger
SwingerDinger

Reputation: 276

Counting the amount of sections in tableView

I want show the amount of sections of a tableView somewhere as a badge.

The first problem is that I get a outcome of 0.

I've declared a int in the TableViewController.h as section.

In the .m file:

- (void)viewDidAppear:(BOOL)animated
{
    self.section = [self.tableView numberOfSections];
    NSLog(@"Number of sections in tableView are %d", self.section);
}

Why do I still get a 0 as outcome? Is there somewhere in the numberOfSectionsInTableView I need to count the amount of sections?

Thanks


Update:

In the tableView I set al the scheduledLocalNotifications. I made a set of 2 notifications which are linked to each other. So 2 rows of scheduledLocalNotifications in each section.

This is what I have in the tableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return (int)[[[UIApplication sharedApplication] scheduledLocalNotifications] count]/2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    int rowIndex = (indexPath.section*2) + indexPath.row;

    // Get list of local notifications
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *localNotification = [localNotifications objectAtIndex:rowIndex];


    return cell;
} 

Upvotes: 1

Views: 4592

Answers (1)

Yogendra
Yogendra

Reputation: 1716

You can find number of sections in any tableview after numberOfSectionsInTableView method call. So, initialize your variable in numberOfSectionsInTableView method before return statement.

Upvotes: 2

Related Questions