user1235155
user1235155

Reputation: 457

UITableView Custom Cells Disappearing content after Scrolling

I have seen this being asked here many times but none of the solutions worked for me. Here is my code snippet:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellClassName = @"DemoTableViewCell_sched";
    DemoTableViewCell_sched *cell = [tableView dequeueReusableCellWithIdentifier:CellClassName];

    if (cell == nil)
    {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }
    cell.fooData = [self.bugs objectAtIndex:indexPath.row];    
    return cell;
}

Then I have in ViewDidLoad

    cellLoader = [UINib nibWithNibName:@"DemoTableViewCell_sched" bundle:[NSBundle mainBundle]];

Upvotes: 7

Views: 9140

Answers (10)

Kirk_hehe
Kirk_hehe

Reputation: 489

I had a similar issue several times. It is very disturbing since it might be anything. From wrong layout constraints to much serious problems. In first case: in UITableViewCell I had

contentView.translatesAutoresizingMaskIntoConstraints = false

After removing this line everytnig went to normal.

In seconds case: I had wrong layout constraint set programmatically:

stackView.topAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 16)

The stack element supposed to be set to Top, not to Bottom. After fixing this line everything went normal.

Upvotes: 1

xiadeye
xiadeye

Reputation: 491

In my case,because I have override this function:

- (void)setFrame:(CGRect)frame
{
//    frame.origin.y += 10;
//    frame.size.height -= 10;
    [super setFrame:frame];
}

Upvotes: 0

Rizwan Ahmed
Rizwan Ahmed

Reputation: 968

Make sure you have implemented heightForRowAtIndexPath delegate method and also make sure that your constraints are properly set.

Upvotes: 1

wcobbett
wcobbett

Reputation: 211

In my case, what was happening was that I had not kept a strong pointer to the tableview's dataSource. So when the tableview was scrolled, the datasource had already been freed and set to nil, which led to the tableview getting 0 for numRowsForSection and nils for cellForRowAtIndexPath.

If anyone has a similar problem, you might look if your datasource and/or custom objects are retained properly.

Upvotes: 21

user4034301
user4034301

Reputation:

Are you adding the UITableViewcontroller as addSubView ? Then you should do like this :

1) addChildViewController,then
2) addSubview
The problem is delegate and datasource of the tableview getting nil since the parent controller unable to a reference to the UITableViewController.

Upvotes: 14

skypirate
skypirate

Reputation: 673

This behavior in TableView had got me head banging literally. Turns out I had used

@property (weak, nonatomic) NSMutableArray *itemsArray;

instead of

@property (strong, nonatomic) NSMutableArray *itemsArray;

Hope this information helps.

Upvotes: -1

0xKayvan
0xKayvan

Reputation: 376

I know this question already has an answer, but just for googlers:

In my case the problem was that the views were still in the UITableViewCell but after scrolling due to a mistake in the frame property of the views, they went under the next UITableViewCell and I could not see them.

I think as the accepted answer has clearly said that :

The problem went away when either:

  • The table view contains many cells (10+),

  • Each cell has a height of more than 50.

he had similar problem to mine.

Upvotes: 1

Matej Ukmar
Matej Ukmar

Reputation: 2245

In my case the problem was the following:

  1. I had a special case where view controller's view was not pushed or presented modally but I've added it as a subview (view.addSubview) to another view.

  2. This way the actual view was retained by its superview, but ViewController object (which was DataSource for table view) was not retained and went out of memory.

  3. I had to assign view controller to a (strong) variable so it stayed in memory.

Upvotes: 6

Saravana Vijay
Saravana Vijay

Reputation: 572

You need to calculate the correct height for each cells according to its content.

This can be done in the UITableView delegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       CGFloat height;
       //Calculate height for each cell
       return height;
}

Upvotes: -1

user1235155
user1235155

Reputation: 457

The problem went away when either:

  1. The table view contains many cells (10+),

  2. Each cell has a height of more than 50.

Weird.

Upvotes: 4

Related Questions