Skip to content

Commit

Permalink
perform configuration inside a performWithoutAnimation closure by def…
Browse files Browse the repository at this point in the history
…ault
  • Loading branch information
mbuchetics committed May 25, 2016
1 parent d4fbbdd commit eddf011
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
35 changes: 26 additions & 9 deletions DataSource/TableViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class TableViewDataSource: NSObject {

/// Whether to show section titles
public var showSectionFooters: Bool?

/// Whether cells should be configured inside a UIView.performWithoutAnimation closure
public var configureWithoutAnimations: Bool = true

/// Optional closure which is called after a cell is dequeued, but before it's being configured (e.g. to "reset" a cell)
public var prepareCell: ((UITableViewCell, indexPath: NSIndexPath) -> Void)?
Expand Down Expand Up @@ -79,17 +82,31 @@ extension TableViewDataSource: UITableViewDataSource {

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let row = dataSource.rowAtIndexPath(indexPath)

if let configurator = configuratorForRowIdentifier(row.identifier) {
let cell = tableView.dequeueReusableCellWithIdentifier(configurator.cellIdentifier, forIndexPath: indexPath)
prepareCell?(cell, indexPath: indexPath)
configurator.configureRow(row, cell: cell, indexPath: indexPath)
return cell

let configure: () -> UITableViewCell = {
if let configurator = self.configuratorForRowIdentifier(row.identifier) {
let cell = tableView.dequeueReusableCellWithIdentifier(configurator.cellIdentifier, forIndexPath: indexPath)
self.prepareCell?(cell, indexPath: indexPath)
configurator.configureRow(row, cell: cell, indexPath: indexPath)
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(row.identifier, forIndexPath: indexPath)
self.prepareCell?(cell, indexPath: indexPath)
return cell
}
}

var cell: UITableViewCell? = nil

if configureWithoutAnimations {
UIView.performWithoutAnimation {
cell = configure()
}
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(row.identifier, forIndexPath: indexPath)
prepareCell?(cell, indexPath: indexPath)
return cell
cell = configure()
}

return cell!
}

public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
Expand Down
2 changes: 1 addition & 1 deletion Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@
395D14A51B90612D00658680 /* DataSource */ = {
isa = PBXGroup;
children = (
39D82E581C04AC2C002A7B1B /* Extensions */,
395D14A61B90612D00658680 /* DataSource.h */,
39D82E491C049ABE002A7B1B /* DataSource.swift */,
39D82E581C04AC2C002A7B1B /* Extensions */,
395D14A81B90612D00658680 /* Info.plist */,
3949A38E1C0706A200715B7F /* TableViewCellConfigurator.swift */,
39D82E4D1C049C34002A7B1B /* TableViewDataSource.swift */,
Expand Down
4 changes: 2 additions & 2 deletions Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1</string>
<string>1.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>5</string>
<string>6</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down

0 comments on commit eddf011

Please sign in to comment.