Jonathan Cabrera
Jonathan Cabrera

Reputation: 1751

UITableView showing inconsistent number of rows compared to data source

I'm trying to make a UITableView present a determined number of rows for a section, but even when I verify that its data source is returning x number of rows for numberOfRowsInSection, the table view shows x-1.

The exception to this unexpected behavior is if the numberOfRowsInSection is less than 3.

I've even put a breakpoint in cellForRowAtIndexPath and I confirmed it's being called for the row that is not appearing.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == SectionNumber_One) {
        return 6;
    } else {
        return self.numberOfProjectRows; // This returns x, but x-1 rows are being shown
    }
}

For example, if self.numberOfProjectRows is 5, only 4 rows are shown for the second section.

If I increase it manually to 6, it shows 5 rows but the data that should be in the 5th position, isn't there.

It doesn't seem to be related to screen size as I tested it on an iPad with same results.

Why is this happening? Is there some other possible modifier of the number of rows in a section?

I'm attaching an screenshot if it's of any help.

UITableView screenshot

EDIT - Here are my delegate methods:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Cell with reuse identifier setup

    if (section == SectionNumber_One) {
        // cell setup for section one that's showing up ok
    } else if (section == SectionNumber_Two) {
        UITextField *projectField = cell.projectTextField;
        if ([self.userProjectKeys count] > row) {
            projectField.text = self.availableProjects[self.userProjectKeys[row]];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Hide the password row for existing users
    if (indexPath.row == FieldTag_Password && ![self.user.key vol_isStringEmpty]) {
        return 0.0f;
    } else {
        return UITableViewAutomaticDimension;
    }
}

Upvotes: 0

Views: 765

Answers (2)

user5890979
user5890979

Reputation:

The problem is probably not in your Datasource methods, but your Delegate methods, tableView(_:heightForRowAt:).

If you do not return a correct height for the cell, it won't show up.

It doesn't matter if you write 1000 cells in your datasource. If you don't return the height, they wont show up.

Upvotes: 1

GeneCode
GeneCode

Reputation: 7588

You are not comforming to the MVC pattern in implementing the table. You must return the count of the tableView's datasource, not variables of it.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == SectionNumber_One) {
        return 6;
    } else {
        return [_displayingArray count]; // <-- here
    }
}

If you want different items for each sections, then declare different datasource (ie array) for each of them.

EDIT: Even returning constant 6 is dangerous in the other section - you ought to add items to another fixed array and return that array's count in this delegate.

Upvotes: 0

Related Questions