Giter VIP home page Giter VIP logo

uitableviewcell-lifecycle-swift's Introduction

Description

The UITableViewCell's swift extension of UITableViewCellLifecycleExtension (with MDTableViewDelegate) provide lifecycle events for UITableViewCell, just like similar events of UIViewController:

  • cellWillAppear
  • cellDidAppear
  • cellDidLayoutSubviews
  • cellDidDisappear

Debug log

Why

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.

How to Use

see the sample code

  • Include UITableViewCellLifecycleExtension.swift and MDTableViewDelegate.swift 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.
  • IMPORTANT Be sure to call super.tableView(tableView, willDisplayCell: cell, forRowAtIndexPath: indexPath) when implementing an override version of tableView:willDisplayCell:forRowAtIndexPath in your subclass of MDTableViewDelegate
  • If UITableViewController's cells want to have these events, when impelementing the delegate within the table view controller, please refer to the implementation of UITableViewController+Lifecycle.h/m in the Objective-C version (see below) and MDTableViewDelegate.swift

Objective-C version

UITableViewCell-Lifecycle

The difference between Objective-C version and Swift version is:   
the Objective-C version swizzle method `tableView:willDisplayCell:forRowAtIndexPath:` in all the subclass of `MDTableViewDelegate`, not `MDTableViewDelegate` itself.
By doing this, there is not need to call `super.tableView(tableView, willDisplayCell: cell, forRowAtIndexPath: indexPath)`, and all the hacking details are hidden.

Known Issues

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.

中文说明

给TableViewCell加上生命周期事件(如cellWillAppear)(下)

uitableviewcell-lifecycle-swift's People

Contributors

madongchunqiu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.