tableview cell disappears when scrolling

4.6k views Asked by At

This problem does not necessarily occur

Sometimes it disappears when sliding and sometimes does not disappear

more information here

click to show gif

click to show storyboard png

code

HSubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:kSubjectCellIdentifier forIndexPath:indexPath];
_subjectView = cell;
cell.delegate = self;
cell.subject = self.subjectArr[indexPath.section - 3];
return cell;
1

There are 1 answers

5
Edwin Finch On BEST ANSWER

Table views "hide" subviews the instant that cell's frame go out of view of the scroll view.

An simple example on how table views work

Let's say you define your row to have a height of 100.0f. You add a simple UIView to it (let's say it has an orange background colour), which pins its edges perfectly to each edge of that UITableViewCell's contentView.

When you scroll that view up/down far enough so the 100.0f height is above/below the scroll view's frame, that row is essentially hidden. This is to make the table view snappy and efficient.

What you are experiencing

Let's use the previous example, though instead of pinning the bottom of the subview to the bottom of the UITableViewCell's contentView, you make it go 50.0f over the bottom of the contentView, therefore being outside of the UITableViewCell.

You'll notice, if you scroll the orange UIView so it goes towards the top, that it disappears even though there is still some orange showing on the screen.

But why is this happening?

Because the subview you added does not fit within the UITableViewCell, when that cell scrolls out of view (based on its frame), it "disappears". Along with it disappearing, your subviews will too.

How do I fix this?

Ensure that all of your subviews are within the frame of the UITableViewCell which it is added to.

A simple way to see these bounds making the background colour of your UITableViewCell's contentView a colour that stands out, such as [UIColor orangeColor];. Then you'll better understand how the frames are all relating.