@@ -257,39 +257,30 @@ open class ScrollStack: UIScrollView {
257257 /// - Parameter animated: `true` to perform animated removeal, by default is `false`.
258258 open func removeAllRows( animated: Bool = false ) {
259259 rows. forEach {
260- removeRow ( $0, animated: animated)
260+ removeRowFromStackView ( $0, animated: animated)
261261 }
262262 }
263263
264- /// Remove row at given index and return removed managed controller (if any).
265- ///
266- /// - Parameter index: index of the row to remove.
267- /// - Parameter animated: `true` to perform animation to remove item, by default is `false`.
268- @discardableResult
269- open func removeRowAtIndex( _ index: Int , animated: Bool = false ) -> UIViewController ? {
270- guard index >= 0 , index < rows. count else {
271- return nil
272- }
273- return removeRow ( rows [ index] )
274- }
275-
276264 /// Remove specified row.
277265 ///
278266 /// - Parameter row: row instance to remove.
279267 /// - Parameter animated: `true` to perform animation to remove item, by default is `false`.
280268 @discardableResult
281- open func removeRow( _ row: ScrollStackRow , animated: Bool = false ) -> UIViewController ? {
269+ open func removeRow( index: Int , animated: Bool = false ) -> UIViewController ? {
270+ guard let row = safeRowAtIndex ( index) else {
271+ return nil
272+ }
282273 return removeRowFromStackView ( row, animated: animated)
283274 }
284275
285276 /// Remove passed rows.
286277 ///
287- /// - Parameter rows: rows to remove.
278+ /// - Parameter rowIndexes: indexes of the row to remove.
288279 /// - Parameter animated: `true` to animate the removeal, by default is `false`.
289280 @discardableResult
290- open func removeRows( _ rows : [ ScrollStackRow ] , animated: Bool = false ) -> [ UIViewController ] ? {
291- return rows . compactMap {
292- return removeRowFromStackView ( $0 , animated: animated)
281+ open func removeRows( indexes rowIndexes : [ Int ] , animated: Bool = false ) -> [ UIViewController ] ? {
282+ return rowIndexes . compactMap {
283+ return removeRowFromStackView ( safeRowAtIndex ( $0 ) , animated: animated)
293284 }
294285 }
295286
@@ -299,15 +290,15 @@ open class ScrollStack: UIScrollView {
299290 /// - Parameter controller: view controller to replace.
300291 /// - Parameter animated: `true` to animate the transition.
301292 /// - Parameter completion: optional callback called at the end of the transition.
302- open func replaceRowAtIndex ( _ sourceIndex: Int , withRow controller: UIViewController , animated: Bool = false , completion: ( ( ) -> Void ) ? = nil ) {
293+ open func replaceRow ( index sourceIndex: Int , withRow controller: UIViewController , animated: Bool = false , completion: ( ( ) -> Void ) ? = nil ) {
303294 guard sourceIndex >= 0 , sourceIndex < rows. count else {
304295 return
305296 }
306297
307298 let sourceRow = rows [ sourceIndex]
308299
309300 guard animated else {
310- removeRow ( sourceRow)
301+ removeRow ( index : sourceRow. index! )
311302 createRowForController ( controller, insertAt: sourceIndex, animated: false )
312303 return
313304 }
@@ -332,7 +323,7 @@ open class ScrollStack: UIScrollView {
332323 /// - Parameter destIndex: destination index.
333324 /// - Parameter animated: `true` to animate the transition.
334325 /// - Parameter completion: optional callback called at the end of the transition.
335- open func moveRowAtIndex ( _ sourceIndex: Int , to destIndex: Int , animated: Bool = false , completion: ( ( ) -> Void ) ? = nil ) {
326+ open func moveRow ( index sourceIndex: Int , to destIndex: Int , animated: Bool = false , completion: ( ( ) -> Void ) ? = nil ) {
336327 guard sourceIndex >= 0 , sourceIndex < rows. count, destIndex < rows. count else {
337328 return
338329 }
@@ -361,11 +352,15 @@ open class ScrollStack: UIScrollView {
361352 /// Hide/Show row from the stack.
362353 /// Row is always on stack and it's returned from the `rows` property.
363354 ///
364- /// - Parameter row : target row.
355+ /// - Parameter rowIndex : target row index .
365356 /// - Parameter isHidden: `true` to hide the row, `false` to make it visible.
366357 /// - Parameter animated: `true` to perform animated transition.
367358 /// - Parameter completion: completion callback called once the operation did finish.
368- open func setRowHidden( _ row: ScrollStackRow , isHidden: Bool , animated: Bool , completion: ( ( ) -> Void ) ? = nil ) {
359+ open func setRowHidden( index rowIndex: Int , isHidden: Bool , animated: Bool , completion: ( ( ) -> Void ) ? = nil ) {
360+ guard let row = safeRowAtIndex ( rowIndex) else {
361+ return
362+ }
363+
369364 guard animated else {
370365 row. isHidden = isHidden
371366 return
@@ -384,13 +379,13 @@ open class ScrollStack: UIScrollView {
384379 /// Hide/Show selected rows.
385380 /// Rows is always on stack and it's returned from the `rows` property.
386381 ///
387- /// - Parameter rows: target rows .
382+ /// - Parameter rowIndexes: indexes of the row to hide or show .
388383 /// - Parameter isHidden: `true` to hide the row, `false` to make it visible.
389384 /// - Parameter animated: `true` to perform animated transition.
390385 /// - Parameter completion: completion callback called once the operation did finish.
391- open func setRowsHidden( _ rows : [ ScrollStackRow ] , isHidden: Bool , animated: Bool , completion: ( ( ) -> Void ) ? = nil ) {
392- rows . forEach {
393- setRowHidden ( $0, isHidden: isHidden, animated: animated)
386+ open func setRowsHidden( indexes rowIndexes : [ Int ] , isHidden: Bool , animated: Bool , completion: ( ( ) -> Void ) ? = nil ) {
387+ rowIndexes . forEach {
388+ setRowHidden ( index : $0, isHidden: isHidden, animated: animated)
394389 }
395390 }
396391
@@ -427,25 +422,25 @@ open class ScrollStack: UIScrollView {
427422 ///
428423 /// - Parameter row: target row.
429424 /// - Parameter insets: new insets.
430- open func setRowInsets( _ row : ScrollStackRow , insets: UIEdgeInsets ) {
431- row . rowInsets = insets
425+ open func setRowInsets( index rowIndex : Int , insets: UIEdgeInsets ) {
426+ safeRowAtIndex ( rowIndex ) ? . rowInsets = insets
432427 }
433428
434429 /// Set the ints of the row's content related to the parent row cell.
435430 ///
436431 /// - Parameter row: target rows.
437432 /// - Parameter insets: new insets.
438- open func setRowsInsets( _ row : [ ScrollStackRow ] , insets: UIEdgeInsets ) {
439- row . forEach {
440- setRowInsets ( $0, insets: insets)
433+ open func setRowsInsets( indexes rowIndexes : [ Int ] , insets: UIEdgeInsets ) {
434+ rowIndexes . forEach {
435+ setRowInsets ( index : $0, insets: insets)
441436 }
442437 }
443438
444439 /// Return the visibility status of a row.
445440 ///
446- /// - Parameter row: row to check.
447- open func isRowVisible( _ row : ScrollStackRow ) -> RowVisibility {
448- guard row. isHidden == false else {
441+ /// - Parameter index: index of the row to check.
442+ open func isRowVisible( index : Int ) -> RowVisibility {
443+ guard let row = safeRowAtIndex ( index ) , row. isHidden == false else {
449444 return . hidden
450445 }
451446
@@ -460,18 +455,22 @@ open class ScrollStack: UIScrollView {
460455 /// Return `true` if row is currently hidden.
461456 ///
462457 /// - Parameter row: row to check.
463- open func isRowHidden( _ row : ScrollStackRow ) -> Bool {
464- return row . isHidden
458+ open func isRowHidden( index : Int ) -> Bool {
459+ return safeRowAtIndex ( index ) ? . isHidden ?? false
465460 }
466461
467462 // MARK: - Scroll
468463
469464 /// Scroll to the passed row.
470465 ///
471- /// - Parameter row: row to make visible.
466+ /// - Parameter rowIndex: index of the row to make visible.
472467 /// - Parameter location: visibility of the row, location of the center point.
473468 /// - Parameter animated: `true` to perform animated transition.
474- open func scrollToRow( _ row: ScrollStackRow , at position: ScrollPosition = . automatic, animated: Bool = true ) {
469+ open func scrollToRow( index rowIndex: Int , at position: ScrollPosition = . automatic, animated: Bool = true ) {
470+ guard let row = safeRowAtIndex ( rowIndex) else {
471+ return
472+ }
473+
475474 let rowFrame = convert ( row. frame, to: self )
476475
477476 if case . automatic = position {
@@ -522,6 +521,14 @@ open class ScrollStack: UIScrollView {
522521 didChangeAxis ( axis)
523522 }
524523
524+ private func safeRowAtIndex( _ index: Int ) -> ScrollStackRow ? {
525+ guard index >= 0 , index < rows. count else {
526+ return nil
527+ }
528+ return rows [ index]
529+ }
530+
531+
525532 /// Remove passed row from stack view.
526533 ///
527534 /// - Parameter row: row to remove.
0 commit comments