Table view cell only masks correctly after scrolling off and then back on

后端 未结 6 1608
無奈伤痛
無奈伤痛 2021-02-04 06:08

I\'m modifying a table view cell during a call to tableView:cellForIndexPath, but the modifications do not appear until the cell is scrolled off and then back on. I

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 06:43

    Reload the tableView in viewDidAppear or viewDidLayoutSubviews method will make it to work.

    -(void)viewDidAppear:(BOOL)animated
    {
     [super viewDidAppear:animated];
     [self.tableView reloadData];
    }
    

    Reason why your code not worked: Actually, as per view life cycle, the methods are called in following manner, init,loadView,viewDidLoad,viewWillAppear,viewWillLayoutSubviews(called more than once),viewDidLayoutSubviews(called multiple time) and lastlyviewDidAppear.

    Now, actually the tableview get loaded in method viewDidLoad, whereas the autolayout constraints that you apply were going to be applied after that method. Thats why, it is not showing expected result. Reloading it again after autolayout constraints get applied make it work. Thats'why above code will surely work.

    One more point to note: In storyboard if you have designed your viewController by using iphone 5 size and then if you run the code in iphone 5, then without reloading it, it must work, but if you run it in any other sized viewController, then it will not work. The reason behind it is, viewDidLoad method get called, then any view have size that was in its storyboard.

    If you have any doubts, let me know

提交回复
热议问题