YichenBman
YichenBman

Reputation: 5651

DeqeueReusableCellWithIdentifier overlapping two cells

In the UITableViewDataSource method, I check to see if I have data:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (data.count == 0)
    {
       return 1; //This one cell will be of type NoDataTypeCell
    } else {
       return data.count //These cells will be of type DataTypeCell
    }
}

so at cellForRowAtIndexPath I check for which type of cells to use, in the same way:

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

    SAFindSuggestedTableViewCell* dataCell = (SAFindSuggestedTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"hasDataIdentifier" forIndexPath:indexPath];

    if (data.count == 0)
    {
        NODataTypeCell* cellNoData = (NODataTypeCell*)[tableView dequeueReusableCellWithIdentifier:@"noDataIdentifier];
        //Setup the noDataCell
        return cellNoData;

    } else {
        return dataCell;
    }
}

This results in something I've never seen before. As expected my controller loads with data being empty. As a result, 1 NODataTypeCell is dequeued.

Once the data comes in, TableView is reloaded.

numberOfRowsInSection returns data.count

cellForRowAtIndexPath does NOT return noDataCell, it returns data.count amount of dataCells

Except my tableView still has a noDataCell. Weirdest part? its directly over the first cell in the tableView. The Cell is unselectable, and also makes the cell behind it unselectable. It has no data, as it has not been setup. But its there, and it won't go away.

What am I doing wrong?

Here is a picture of the view debugger perspective behind the cells. You can clearly see that there are two cells are the first row, and only one cell on the second row enter image description here

Upvotes: 1

Views: 274

Answers (1)

vien vu
vien vu

Reputation: 4337

tableView dequeueReusableCellWithIdentifier: will return an object cell and add to tableView.

In your code above:

 SAFindSuggestedTableViewCell* dataCell = (SAFindSuggestedTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"hasDataIdentifier" forIndexPath:indexPath];

if (data.count == 0)
{
    NODataTypeCell* cellNoData = (NODataTypeCell*)[tableView dequeueReusableCellWithIdentifier:@"noDataIdentifier];

In case data = 0. Two cell already add to same indexpath of tableView. So it's reason for bug.

You should separate your code to return exact one cell in indexPath like:

if (data.count == 0)
{
    NODataTypeCell* cellNoData = (NODataTypeCell*)[tableView dequeueReusableCellWithIdentifier:@"noDataIdentifier];
    //Setup the noDataCell
    return cellNoData;

} else {
    SAFindSuggestedTableViewCell* dataCell = (SAFindSuggestedTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"hasDataIdentifier" forIndexPath:indexPath];
    return dataCell;
}

I see that you comment about crash. I think it's because your logic with your real code.

Upvotes: 2

Related Questions