Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Total number of rows in UITableView with multiple sections

I need to calculate the total number of rows in my UITableView, however I have multiple sections. Is there a property on the table view I can access? If not, how can I find this information?

like image 881
Jules Avatar asked Jan 20 '26 14:01

Jules


2 Answers

Create an "Objective-c Category" of UITableView from the new file dialogue of xCode and add this method to it:

-(NSInteger)numberOfRowsInTotal{
    NSInteger sections = self.numberOfSections;
    NSInteger cellCount = 0;
    for (NSInteger i = 0; i < sections; i++) {
        cellCount += [self numberOfRowsInSection:i];
    }

    return cellCount;
}

Then import your category header into any class that you need to access this method in et voila, you have what you're looking for.

like image 53
jackslash Avatar answered Jan 23 '26 03:01

jackslash


You can easily calculate this value by iterating through each section.

NSUInteger allRows = 0;
for(NSInteger i = 0; i < [tableview numberOfSections]; i++)
{
        allRows += [tableview numberOfRowsInSection:i];
}

If you would like a property you can easily add the above code to a category for UITableView.

like image 41
Joe Avatar answered Jan 23 '26 04:01

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!