UITableView Custom Cells Disappearing content after Scrolling

前端 未结 10 1128
滥情空心
滥情空心 2020-12-17 00:12

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 *)         


        
相关标签:
10条回答
  • 2020-12-17 00:23

    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.

    0 讨论(0)
  • 2020-12-17 00:25

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

    0 讨论(0)
  • 2020-12-17 00:29

    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.

    0 讨论(0)
  • 2020-12-17 00:31

    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;
    }
    
    0 讨论(0)
  • 2020-12-17 00:32

    In my case,because I have override this function:

    - (void)setFrame:(CGRect)frame
    {
    //    frame.origin.y += 10;
    //    frame.size.height -= 10;
        [super setFrame:frame];
    }
    
    
    0 讨论(0)
  • 2020-12-17 00:34

    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.
    
    0 讨论(0)
提交回复
热议问题