Allen
Allen

Reputation: 6882

Is there anyway to get the rows count of current tableView?

Not visibleCells, the total rows count.

I know there is a datasource array, but is it possible to obtain without concerning data?

Somebody said this question statement is unclear, so update my situation: I want to insert some rows into tableView, one common way is reassemble the datasource array then use [tableView reloadData] to refresh whole tableView, but I don't wanna do this because of user experiments. Instead I try to use insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation method to just insert the new rows, so one must-do thing is to create the indexPaths. I choose [NSIndexPath indexPathWithIndex:], so you know I should get the previous number of rows then generate indexPaths array and before invoking insertRowsAtindexPaths:withRowAnimation: update the datasource array. It is important for opportunity to update the datasource array by this way. It sucks i think, so I'm a little curious "Is there anyway to get the rows count of current tableView?"

Upvotes: 0

Views: 8297

Answers (5)

ashif-ismail
ashif-ismail

Reputation: 1076

swift version : yourTableView.numberOfRows(inSection: 0)

Upvotes: 4

lePapa
lePapa

Reputation: 377

In swift 3:

func getAllRowCount()->Int{
    var rowCount = 0
    for index in 0...self.tableView.numberOfSections-1{
        rowCount += self.tableView.numberOfRows(inSection: index)
    }
    return rowCount
}

Upvotes: 2

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

To get the total number of rows in your table view, loop through each section in the table view and increment the rowCount by the number of rows in the current section:

NSInteger rowCount = 0;

for (NSInteger i = 0 ; i < tableView.numberOfSections ; i ++) {
    rowCount += [tableView numberOfRowsInSection:i];
}

Upvotes: 7

Sudhin Davis
Sudhin Davis

Reputation: 2020

Simply use [TableView numberOfRowsInSection:yourSection];

Upvotes: 0

Zhang
Zhang

Reputation: 11607

If all your table rows have the same height, then you probably can go:

row count = tableView.contentSize.height / tableView.rowHeight

But if you're tableView has different row heights, I can't help you :D

Upvotes: -3

Related Questions