0

In the layoutSubviews of uitableviewcell, there is logics like

switch(enum_instance){ }

enum_instance was changed from some place, for instance in one timer, then setNeedLayout to trigger layoutSubviews

But the enum_instance in layoutSubviews will not be updated. Is this bug ? or something wrong I have done ?

Thanks,

Forrest
  • 122,703
  • 20
  • 73
  • 107
  • `setNeedLayout` will only schedule redraw for new cycle. Calling `layoutIfNeeded` will lays out the subviews immediately. – XeNoN Apr 30 '15 at 07:39
  • actually I am using layoutIfNeeded :) the local variable will be updated as unexpected sometimes, that is odd – Forrest Apr 30 '15 at 07:52

1 Answers1

0

This happens because UITableViewCells are recycled. Scrolling tableView will shuffle cells when dequeueReusableCellWithIdentifier: is called. This means that layoutIfNeeded my be called on wrong cell (after scrolling, that cell may be assigned to another indexPath). Only secure place to setup cell property is in tableView:cellForRowAtIndexPath: where cell should be configured.

Simpler way is to recalculate cell within tableView:heightForRowAtIndexPath: and tableView:cellForRowAtIndexPath:. This methods can be re-triggered by calling reloadRowsAtIndexPaths:withRowAnimation: providing array of indexPaths for cells that you want to redraw.

XeNoN
  • 694
  • 6
  • 16