Skip to content

Commit 4f4df2b

Browse files
Update BBTableView scrollToRow
1 parent 795dbb9 commit 4f4df2b

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

BBSwiftUIKit/BBSwiftUIKit/BBTableView.swift

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ public extension BBTableView {
2525
return view
2626
}
2727

28-
func bb_scrollToRow(_ scrollToRow: Binding<BBTableViewScrollToRowParameter?>) -> Self {
28+
func bb_scrollToRow(_ row: Binding<Int?>, position: UITableView.ScrollPosition, animated: Bool) -> Self {
2929
var view = self
30-
view._scrollToRow = scrollToRow
30+
view._scrollToRow = row
31+
view.scrollToRowPosition = position
32+
view.scrollToRowAnimated = animated
3133
return view
3234
}
3335

@@ -76,25 +78,17 @@ public extension BBTableView {
7678
}
7779
}
7880

79-
public struct BBTableViewScrollToRowParameter {
80-
public let row: Int
81-
public let position: UITableView.ScrollPosition
82-
public let animated: Bool
83-
84-
public init(row: Int, position: UITableView.ScrollPosition, animated: Bool) {
85-
self.row = row
86-
self.position = position
87-
self.animated = animated
88-
}
89-
}
90-
9181
public struct BBTableView<Data, Content>: UIViewControllerRepresentable, BBUIScrollViewRepresentable where Data : RandomAccessCollection, Content : View, Data.Element : Equatable {
9282
let data: Data
9383
let content: (Data.Element) -> Content
9484

9585
@Binding public var reloadData: Bool
9686
@Binding public var reloadRows: [Int]
97-
@Binding public var scrollToRow: BBTableViewScrollToRowParameter?
87+
88+
@Binding public var scrollToRow: Int?
89+
public var scrollToRowPosition: UITableView.ScrollPosition = .none
90+
public var scrollToRowAnimated: Bool = true
91+
9892
@Binding public var contentOffset: CGPoint
9993
@Binding public var contentOffsetToScrollAnimated: CGPoint?
10094
public var isPagingEnabled: Bool = false
@@ -230,8 +224,8 @@ private class _BBTableViewController<Data, Content>: UIViewController, UITableVi
230224

231225
representable.updateTableView?(tableView)
232226

233-
if let scrollToRow = representable.scrollToRow {
234-
tableView.scrollToRow(at: IndexPath(row: scrollToRow.row, section: 0), at: scrollToRow.position, animated: scrollToRow.animated)
227+
if let row = representable.scrollToRow {
228+
tableView.scrollToRow(at: IndexPath(row: row, section: 0), at: representable.scrollToRowPosition, animated: representable.scrollToRowAnimated)
235229
DispatchQueue.main.async { [weak self] in
236230
guard let self = self else { return }
237231
self.representable.scrollToRow = nil

BBSwiftUIKitDemo/BBSwiftUIKitDemo/TableViewExample.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct TableViewExample: View {
1414
@State var updateHeight = false
1515
@State var reloadData = false
1616
@State var reloadRows: [Int] = []
17-
@State var scrollToRow: BBTableViewScrollToRowParameter? = nil
17+
@State var scrollToRow: Int? = nil
1818
@State var contentOffset: CGPoint = .zero
1919
@State var contentOffsetToScrollAnimated: CGPoint? = nil
2020
@State var isRefreshing: Bool = false
@@ -39,7 +39,7 @@ struct TableViewExample: View {
3939
}
4040
.bb_reloadData($reloadData)
4141
.bb_reloadRows($reloadRows)
42-
.bb_scrollToRow($scrollToRow)
42+
.bb_scrollToRow($scrollToRow, position: .none, animated: true)
4343
.bb_contentOffset($contentOffset)
4444
.bb_contentOffsetToScrollAnimated($contentOffsetToScrollAnimated)
4545
.bb_setupRefreshControl { refreshControl in
@@ -70,13 +70,13 @@ struct TableViewExample: View {
7070

7171
Button("Reload data") {
7272
self.reloadListData()
73-
self.scrollToRow = BBTableViewScrollToRowParameter(row: 0, position: .top, animated: true)
73+
self.scrollToRow = 0
7474
}
7575
.padding()
7676

7777
Button("Reload rows") {
7878
self.reloadListRows()
79-
self.scrollToRow = BBTableViewScrollToRowParameter(row: 0, position: .top, animated: true)
79+
self.scrollToRow = 0
8080
}
8181
.padding()
8282
}

0 commit comments

Comments
 (0)