IOS - 如何创建带有水平滚动列表的 TableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27844684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:16:41  来源:igfitidea点击:

IOS - How to create TableView with horizontal scrollable lists

iosuitableviewswiftlayout

提问by Shlomi Schwartz

I'm trying to implement a table view with scrollable horizontal items (with unknown number of items) like the illustration below:

我正在尝试使用可滚动的水平项目(项目数量未知)来实现一个表格视图,如下图所示:

enter image description here

在此处输入图片说明

The UI should behave like so:

用户界面应该像这样:

  1. At the init state the table cell shows a label and some text and the circle item pops out of the right side
  2. If the users swipes from right to left, the label and text fades out and the horizontal list (inside the cell) takes it's place
  1. 在 init 状态,表格单元格显示一个标签和一些文本,圆形项目从右侧弹出
  2. 如果用户从右向左滑动,标签和文本淡出,水平列表(单元格内)取而代之


I thought about using TableView and dequeueReusableCellWithIdentifierand creating a prototype cell, but then I need to remember the list horizontal position and init the cell properly on cellForRowAtIndexPathand that will probably affect performance.

我想过使用 TableView 并dequeueReusableCellWithIdentifier创建一个原型单元格,但是我需要记住列表水平位置并正确初始化单元格cellForRowAtIndexPath,这可能会影响性能。

Q:What layout would you use in order to achieve this goal, any input / tutorial would be appreciated

问:为了实现这个目标,你会使用什么布局,任何输入/教程将不胜感激

采纳答案by Andrew Robinson

Use a UITableViewand add a UICollectionViewto your reusable tableView cell. UICollectionViewworks similar to UITableViewin that the "items" (like cells) are reusable and instantiated only when they will appear on screen. I did a quick google search for tutorials and found UICollectionView in UITableViewCell. Check that out and a few other StackOverflow questions regarding design and you should be golden.

使用 aUITableView并将 a 添加UICollectionView到您的可重用 tableView 单元格。UICollectionView工作原理类似于UITableView“项目”(如单元格)仅在它们出现在屏幕上时才可重用和实例化。我在谷歌上快速搜索了教程,并在 UITableViewCell 中找到了UICollectionView。看看这个和其他一些关于设计的 StackOverflow 问题,你应该是金。

回答by Xiaojun

Using UIScrollViewmay require heavy effort if there are large amount of UI elements inside each scrollview because you have to instantiate all those upfront. A better solution is to define your custom UITableViewCellwhich has its own UITableViewbut rotated in 90 degree for horizontal scrolling. UI elements inside will be created only when they need to be shown by using dequeueReusableCellWithIdentifier:forIndexPath:. Please see some sample code on how to create a 90 degree rotated table view:

UIScrollView如果每个滚动视图中有大量 UI 元素,则使用可能需要付出大量努力,因为您必须预先实例化所有这些元素。更好的解决方案是定义您的自定义UITableViewCell,它有自己的UITableView但旋转 90 度以进行水平滚动。内部的 UI 元素仅在需要使用dequeueReusableCellWithIdentifier:forIndexPath:. 请参阅有关如何创建 90 度旋转表视图的示例代码:

iPhone Tableview Use Cells in Horizontal Scrolling not Vertical

iPhone Tableview 在水平滚动而不是垂直滚动中使用单元格

回答by Calc91

You can use a ScrollView to put inside your tableview and set the values "Direction lock enabled" and "Paging Enabled" to true for the scrollview.

您可以使用 ScrollView 放入 tableview 并将值“Direction lock enabled”和“Paging Enabled”设置为滚动视图的 true 。

回答by Mojtaba Hosseini

- SwiftUI

- SwiftUI

In swiftUI, you can use the power of Stacks and ScrollViewalongside with List(if you want):

在 swiftUI 中,您可以使用Stacks 和ScrollViewwith的强大功能List(如果您愿意):

struct ContentView: View {

    var verticalData = 0...3
    var horizontalData = 0...15

    var body: some View {
        List(self.verticalData, id: \.self) { vData in
            ScrollView(.horizontal) {
                HStack(spacing: 16) {
                    ForEach(self.horizontalData, id: \.self) { _ in
                        Circle()
                            .foregroundColor(.blue)
                            .frame(width: 40, height: 40, alignment: .center)
                    }
                }
            }
        }
    }
}

In this example I used a Listfor vertical data and a horizontal ScrollViewcontaining a HStack(Horizonl stack) of circles.

在这个例子中,我使用了一个List用于垂直数据和一个ScrollView包含HStack(水平堆栈)圆圈的水平数据。

- Example

- 例子

The following code is the the only code to reproduce a view exactly like your illustration (4 years later on iPhone Xs):

以下代码是唯一可以重现与您的插图完全相同的视图的代码(4 年后在 iPhone Xs 上):

struct CircleView: View {
    var body: some View {
        Circle()
        .foregroundColor(.blue)
        .frame(width: 80, height: 80, alignment: .center)
    }
}

struct RowView: View {
    var body: some View {
        ScrollView(.horizontal) {
            HStack(spacing: 16) {
                VStack(alignment: .leading, spacing: 4) {
                    Text("Label")
                    Text("Some text here")
                }
                ForEach(0...15, id: \.self) { _ in
                    CircleView()
                }
            }
            .padding(16)
        }
    }
}

struct ContentView: View {

    var body: some View {
        List {
            ForEach(0...3, id: \.self) { _ in
                RowView()
            }.listRowInsets(EdgeInsets())
        }
    }
}

- Result

- 结果

enter image description here

在此处输入图片说明