From e85679e84156641a12c8f8d0e2593cccb6181d56 Mon Sep 17 00:00:00 2001 From: Sergey Nikitenko Date: Sun, 3 May 2015 21:05:08 +0300 Subject: [PATCH] Insert/Delete rows methods --- WKInterfaceTableExtensions.swift | 60 ++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/WKInterfaceTableExtensions.swift b/WKInterfaceTableExtensions.swift index 4a832e7..632b7ce 100644 --- a/WKInterfaceTableExtensions.swift +++ b/WKInterfaceTableExtensions.swift @@ -77,17 +77,63 @@ private class GenericRowConfigureBlock : WKInterfaceTable.Builder.RowConfigur extension WKInterfaceTable { - func insertRowAtIndex (index: Int, rowType: String) -> AnyObject! { - self.insertRowsAtIndexes(NSIndexSet(index: index), withRowType: rowType) - return rowControllerAtIndex(index) + func insertRowAtIndex (index: Int, rowType: String) -> AnyObject { + insertRowsAtIndexes(NSIndexSet(index: index), withRowType: rowType) + return rowControllerAtIndex(index)! + } + + func insertRowsAtIndex (startIndex:Int, count: Int, rowType: String) -> [AnyObject] { + let indexes = NSIndexSet(indexesInRange: NSMakeRange(startIndex, count)) + insertRowsAtIndexes(indexes, withRowType: rowType) + var rows: [AnyObject] = [] + for i in 0.. [AnyObject] { + return insertRowsAtIndex(indexOfRow(targetRow)! + 1, count: count, rowType: rowType) } - func insertRowAtIndex (index: Int, rowType: String, configure: (row:T) -> Void ) { - self.insertRowsAtIndexes(NSIndexSet(index: index), withRowType: rowType) - let row = rowControllerAtIndex(index) as! T - configure(row: row) + + func removeRowAtIndex (index: Int) { + removeRowsAtIndexes(NSIndexSet(index: index)) } + func removeRow (row: AnyObject) { + if let rowIndex = indexOfRow(row) { + removeRowsAtIndexes(NSIndexSet(index: rowIndex)) + } + } + + func removeRows (rows: [AnyObject]) { + if rows.count > 0 { + let indices = NSMutableIndexSet() + var rowIndex = 0 + for row in rows { + if row === rowControllerAtIndex(rowIndex) { + indices.addIndex(rowIndex++) + } else if let index = indexOfRow(row) { + indices.addIndex(index) + rowIndex = index + 1 + } + } + removeRowsAtIndexes(indices) + } + } + + + func indexOfRow (row: AnyObject) -> Int? { + for i in 0..