Giter Site home page Giter Site logo

jtssemanticreload's Introduction

JTSSemanticReload

A category method on UITableViewController for calling "reloadData" while preserving semantic content offset.

Why You’d Want This

Many times you don’t want to use an animated table view update, but you do want to insert new rows above the current content offset. The problem with using reloadData is that it loses the user’s current place in the content. In these situations, use the JTSSemanticReload category instead.

Usage

Anywhere you would use:

- (void)reloadData;

replace it with a call to:

- (void)JTS_reloadDataPreservingSemanticContentOffset:(JTSSemanticReloadItemForIndexPath)itemForPathBlock
                                     pathForItemBlock:(JTSSemanticReloadIndexPathForItem)pathForItemBlock;

Here's an example implementation:

- (void)someDataModelDidUpdate:(id)model andStuff:(id)stuff {

  [self JTS_reloadDataPreservingSemanticContentOffset:^id(NSIndexPath *indexPathPriorToReload, UITableViewCell *cellPriorToReload) {
    return (SomeTweet *)[cellPriorToReload tweet];
  } pathForItemBlock:^NSIndexPath *(id dataSourceItem) {
    return [self.tweetController indexPathForTweet:(SomeTweet *)dataSourceItem];
  }];
}

The method accepts two block arguments. The two blocks are called synchronously and may be called multiple times. One of them takes the previous NSIndexPath and UITableViewCell, returning a data source item. The other takes a data source item and returns the new/current NSIndexPath. The data source item can be any object you wish. It's up to your application to be able to derive an updated index path from the data source item and vice versa. For example, if you were writing a Twitter client, the data source item would probably be a tweet.

This method is safe to use with table views that have non-zero content insets, header views, and footer views.

jtssemanticreload's People

Contributors

jaredsinclair avatar irace avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

keslcod jbrayton

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.