Latest 1.0.1
Homepage https://github.com/jaredsinclair/JTSSemanticReload
License MIT
Platforms ios 7.0, requires ARC
Frameworks UIKit
Authors

A category method on UITableView 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.tableView 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.

Latest podspec

{
    "name": "JTSSemanticReload",
    "version": "1.0.1",
    "summary": "A category on UITableViewController for calling "reloadData" while preserving semantic content offset.",
    "homepage": "https://github.com/jaredsinclair/JTSSemanticReload",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Jared Sinclair": "https://twitter.com/jaredsinclair"
    },
    "source": {
        "git": "https://github.com/jaredsinclair/JTSSemanticReload.git",
        "tag": "1.0.1"
    },
    "platforms": {
        "ios": "7.0"
    },
    "requires_arc": true,
    "frameworks": "UIKit",
    "source_files": [
        "Source/*.{h,m}"
    ]
}

Pin It on Pinterest

Share This