[ACCEPTED]-How to find the number of Cells in UITableView-uitableview

Accepted answer
Score: 41
int sections = [tableView numberOfSections]; 

int rows = 0; 

for(int i=0; i < sections; i++)
{
    rows += [tableView numberOfRowsInSection:i];
}

Total Number of rows = rows;

0

Score: 17

the total count of all cells (in a section) should 4 be whatever is being returned by

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

however 3 this method is getting count, you can do 2 it in your own methods also. Probably something 1 like return [myArrayofItems count];

Score: 6

UITableView is designed only as a way to view your 14 data, taken from the data source. The total 13 number of cells is an information that belongs 12 in the data source and you should access 11 it from there. UITableView holds enough cells to fit 10 the screen which you can access using

- (NSArray *)visibleCells

One 9 dirty solution would be to maintain a separate 8 array of every UITableViewCell you create. It works, and 7 if you have a low number of cells it's not 6 that bad.

However, this is not a very elegant 5 solution and personally I wouldn't choose 4 this unless there is absolutely no other 3 way. It's better that you do not modify 2 the actual cells in the table without a 1 corresponding change in the data source.

Score: 4

based on Biranchi's code, here's a little 2 snippet that retrieves every through cell. Hope 1 this can help you !

UITableView *tableview = self.tView;    //set your tableview here
int sectionCount = [tableview numberOfSections];
for(int sectionI=0; sectionI < sectionCount; sectionI++) {
    int rowCount = [tableview numberOfRowsInSection:sectionI];
    NSLog(@"sectionCount:%i rowCount:%i", sectionCount, rowCount);
    for (int rowsI=0; rowsI < rowCount; rowsI++) {
        UITableViewCell *cell = (UITableViewCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowsI inSection:sectionI]];
        NSLog(@"%@", cell);
    }
}
Score: 4

Swift (As of October 2020)

let sections: Int = tableView.numberOfSections
var rows: Int = 0

for i in 0..<sections {
    rows += tableView.numberOfRows(inSection: i)
}

0

Score: 0

Swift 3 equivalent example

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if section == 0 {
            return 1
        }else if section == 1 {    
            return timesArray.count // This returns the cells equivalent to the number of items in the array.
        }
        return 0
    }

0

Score: 0

Extension for UITableView for getting total number 1 of rows. Written in Swift 4

extension UITableView {

    var rowsCount: Int {
        let sections = self.numberOfSections
        var rows = 0

        for i in 0...sections - 1 {
            rows += self.numberOfRows(inSection: i)
        }

        return rows
    }
}

More Related questions