如何在SwiftUI中反转列表 [英] How to make List reversed in SwiftUI

查看:195
本文介绍了如何在SwiftUI中反转列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SwiftUI的新手,试图在Android LinearLayoutManager

I'm new in SwiftUI, trying to make something like reverse in Android LinearLayoutManager

messagesRecyclerView = view.findViewById(R.id.messagesRecyclerView);

manager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, true); // => true means reverse layout
messagesRecyclerView.setLayoutManager(manager);

在这种情况下,一切都从底部到顶部.

in this case everything goes from bottom to top.

我能找到的所有有用的方法是tableview反向: 从底部加载表视图,向上滚动(反向表视图)(iOS)

Everything useful what I can find is tableview reverse: Load tableview from bottom, scroll up (reverse tableview) (iOS)

//In ViewDidLoad
conversationTableView.transform = CGAffineTransform(rotationAngle: -(CGFloat)(Double.pi));

//In cellForRowAtIndexPath
cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));

,但是我不知道如何将其添加到List中(好像它只是TableView的包装). 这里的另一种方法:如何从下往上填充UITableView?

but I don't know how to add it into List (seems like it just a wrapper around TableView). Another approach here: How to populate UITableView from the bottom upwards?

                List {
                    ForEach(itemsData.messages) { item in
                        ChatRow(item: item)
                    }
                    Text("Load more messages...")
                        .onAppear() {
                            print("Load more messages...")
                            self.itemsData.next()
                    }
                }

我的假设是要使用.onAppearoverride方法来实现这一目的.

My assumption is to get something .onAppear or override methods to make this work.

SwiftUI似乎还太年轻而无法深入.

Seems like SwiftUI too young to go that deep.

另一个问题:他们是否有API可以以编程方式在列表中滚动?

希望我没有重复任何问题.

Hope I'm not duplicating any questions.

将其用于匿名聊天应用程序 https://en.lonje.com/

Using it for anonymous chat app https://en.lonje.com/

推荐答案

UITableView上有一个技巧可以翻转表格和每个单元格.因此,它似乎从底部开始填充.您也可以在SwiftUI中做到这一点:

There is a trick on UITableView that you flip the table and each cell. So it appears to be filling from bottom. You can do this trick in SwiftUI too:

完整工作演示:

struct ContentView: View {
    @State private var ids = [String]()

    init() {
        UITableView.appearance().tableFooterView = UIView()
        UITableView.appearance().separatorStyle = .none
    }

    var body: some View {
        ZStack {
            List(ids, id: \.self) { id in
                Text(id).scaleEffect(x: 1, y: -1, anchor: .center)
            }.scaleEffect(x: 1, y: -1, anchor: .center)

            Button("Touch") {
                self.ids.append(UUID().uuidString)
            }
        }
    }
}

这篇关于如何在SwiftUI中反转列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆