SwiftUI List with Section Index on right hand side?

前端 未结 4 1062
旧时难觅i
旧时难觅i 2021-02-03 13:23

Is it possible to have a List with an index on the right hand side, like the example below in SwiftUI?

\"Example\"<

4条回答
  •  不思量自难忘°
    2021-02-03 13:44

    I was looking for a solution to the same question , but it currently the only option that we might have right now is using UITableView as View.

    import SwiftUI
    import UIKit
    
    struct TableView: UIViewRepresentable {
    
        func makeUIView(context: Context) -> UITableView {
            let tableView =  UITableView(frame: .zero, style: .plain)
            tableView.delegate = context.coordinator
            tableView.dataSource  = context.coordinator
            return tableView
        }
    
        func updateUIView(_ uiView: UITableView, context: Context) {
    
        }
    
        func makeCoordinator() -> Coordinator {
            Coordinator()
        }
    
        final class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
            func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                2
            }
    
            func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cellId = "cellIdentifier"
                let cell = tableView.dequeueReusableCell(withIdentifier: cellId) ?? UITableViewCell(style: .default, reuseIdentifier: cellId)
    
                cell.textLabel?.text = "\(indexPath)"
                return cell
            }
    
            func sectionIndexTitles(for tableView: UITableView) -> [String]? {
                ["a", "b"]
            }
        }
    

提交回复
热议问题