简体   繁体   English

在 SwiftUI 应用程序中按下按钮时滚动到特定行的列表

[英]List that scrolls to certain row when button pressed in SwiftUI app

In a SwiftUI app, I'm trying to create a list that automatically scrolls to the next row when a button is pressed.在 SwiftUI 应用程序中,我试图创建一个在按下按钮时自动滚动到下一行的列表。

Apparently, this functionality isn't currently possible with a SwiftUI List, but is with UITableView using the scrollToRowAtIndexPath method as mentioned in part #3 of this answer https://stackoverflow.com/a/58830883/11698443 This answer is well explained but doesn't suggest how to implement what they described as the best solution. Apparently, this functionality isn't currently possible with a SwiftUI List, but is with UITableView using the scrollToRowAtIndexPath method as mentioned in part #3 of this answer https://stackoverflow.com/a/58830883/11698443 This answer is well explained but没有建议如何实施他们所说的最佳解决方案。

Below is some example code of how the list could look in SwiftUI.下面是一些示例代码,说明列表在 SwiftUI 中的外观。 The goal would be for the button to be pressed and then for the list to scroll to the next row.目标是按下按钮,然后列表滚动到下一行。 I think the solution would need to interface with UIKit in order for it to work.我认为该解决方案需要与 UIKit 接口才能使其工作。 It would be awesome if someone could figure out how to do this in the cleanest possible way.如果有人能弄清楚如何以最干净的方式做到这一点,那就太棒了。 It's unfortunate that this can't be done using all SwiftUI.不幸的是,这不能使用所有 SwiftUI 来完成。

Thanks in advance to anyone who thinks that they can solve this problem.提前感谢任何认为他们可以解决这个问题的人。

import SwiftUI

struct ScrollingList: View {
    var body: some View {
        List(0 ..< 10) { item in
            VStack {
                Text("\(item)")

                Button(action: {}) {
                Text("Scroll To Next Item")
                }
            }
            .frame(height: 500)
            .background(Color.blue)
        }
    }
}

check out this nifty library, https://github.com/siteline/SwiftUI-Introspect .查看这个漂亮的库https://github.com/siteline/SwiftUI-Introspect with it you can do this:有了它,你可以这样做:

import Introspect

List {
  Text("some")
  Text("list")
}.introspectTableView { tableView in
          tableView.scrollToRowAtIndexPath(<#IndexPath#>, atScrollPosition: <#UITableView.ScrollPosition#>, animated: <#Bool#>)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM