         return nil
     }
 
-    private func refreshTableSections(oldSections: [Section]) {
-        if let tableView = tableView {
-            let oldCount = oldSections.count
-            let newCount = sections.count
-            let delta = newCount - oldCount
-            let animation: UITableViewRowAnimation = .Automatic
-
-            tableView.beginUpdates()
-
-            if delta == 0 {
-                tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, newCount)), withRowAnimation: animation)
-            } else {
-                if delta > 0 {
-                    // Insert sections
-                    tableView.insertSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, delta)), withRowAnimation: animation)
-                } else {
-                    // Remove sections
-                    tableView.deleteSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, -delta)), withRowAnimation: animation)
-                }
-
-                // Reload existing sections
-                let commonCount = min(oldCount, newCount)
-                tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, commonCount)), withRowAnimation: animation)
-            }
-
-            tableView.endUpdates()
-        }
+    private func refreshTableSections(oldSections: [Section]? = nil) {
+		guard let tableView = tableView else { return }
+		guard let oldSections = oldSections else {
+			tableView.reloadData()
+			return
+		}
+
+		let oldCount = oldSections.count
+		let newCount = sections.count
+		let delta = newCount - oldCount
+		let animation: UITableViewRowAnimation = .Automatic
+
+		tableView.beginUpdates()
+
+		if delta == 0 {
+			tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, newCount)), withRowAnimation: animation)
+		} else {
+			if delta > 0 {
+				// Insert sections
+				tableView.insertSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, delta)), withRowAnimation: animation)
+			} else {
+				// Remove sections
+				tableView.deleteSections(NSIndexSet(indexesInRange: NSMakeRange(oldCount - 1, -delta)), withRowAnimation: animation)
+			}
+
+			// Reload existing sections
+			let commonCount = min(oldCount, newCount)
+			tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, commonCount)), withRowAnimation: animation)
+		}
+
+		tableView.endUpdates()
     }
 
     private func refreshRegisteredCells() {


    // MARK: - Properties

    private weak var tableView: UITableView?
    weak var tableViewDataSource: UITableViewDataSource?
    weak var tableViewDelegate: UITableViewDelegate?

    public var sections = [Section]() {
        didSet {
            refreshTableSections(oldValue)
            refreshRegisteredCells()
        }
    }



    // MARK: - Initializers

    public init(tableView: UITableView? = nil) {
        super.init()
        self.tableView = tableView
        updateTableView()
    }

	deinit {
		tableView = nil
	}


    // MARK: - Private

	private func updateTableView() {
		guard let tableView = tableView else { return }
		tableView.dataSource = self
		tableView.delegate = self
		refresh()
	}

	private func refresh() {
		refreshTableSections()
		refreshRegisteredCells()
	}

    private func sectionForIndex(index: Int) -> Section? {
        if sections.count <= index {
