The UITableViewCell+Lifecycle
category (with MDTableViewDelegate
) provide lifecycle events for UITableViewCell, just like similar events of UIViewController:
- cellWillAppear
- cellDidAppear
- cellDidLayoutSubviews
- cellDidDisappear
I'm a big fan of UITableView, since it provides the flexibility of organizing contents dynamically. And with more and more animations in the UI, I found it hard to locate my code elegantly.
On the other hand, UIViewController is nice with viewWillAppear, viewDidAppear, viewDidLayoutSubviews, viewWillDisappear and viewDidDisappear etc.
I though I shall make this happen to UITableViewCell.
see the sample code
- Preferred: include
UITableViewCell+Lifecycle.h/m
andMDTableViewDelegate.h/m
into the projects. Subclass MDTableViewDelegate, and put your tableView's dataSource and delegate code in this class. And these events will be triggerred in the cells. - NOT recommended: include
UITableViewCell+Lifecycle.h/m
andUITableViewController+CellLifecycle.h/m
int to the projects, and subclass UITableViewController (shall explictly conform to protocol UITableViewDelegate), and these events will be triggerred in the cells.
UITableViewCell-Lifecycle-Swift
The difference between Swift version and Objective-C version is:
the Swift version swizzle method `tableView:willDisplayCell:forRowAtIndexPath:` directly in `MDTableViewDelegate`, not its subclass.
By doing this, the tranverse of all the classes to find classes that need method swizzling is not necessary.
These events won't be triggerred if the whole tableView is going on-screen or off-screen, e.g. when pushing another view controller or poped back from another view controller. (Currently I don't see the need to fix this.)
It only happens when the UITableViewCell is scrolling into or out-of the visible area of the table view.