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
IOS - How to create TableView with horizontal scrollable lists
提问by Shlomi Schwartz
I'm trying to implement a table view with scrollable horizontal items (with unknown number of items) like the illustration below:
我正在尝试使用可滚动的水平项目(项目数量未知)来实现一个表格视图,如下图所示:
The UI should behave like so:
用户界面应该像这样:
- At the init state the table cell shows a label and some text and the circle item pops out of the right side
- 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
- 在 init 状态,表格单元格显示一个标签和一些文本,圆形项目从右侧弹出
- 如果用户从右向左滑动,标签和文本淡出,水平列表(单元格内)取而代之
I thought about using TableView and dequeueReusableCellWithIdentifier
and creating a prototype cell, but then I need to remember the list horizontal position and init the cell properly on cellForRowAtIndexPath
and 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 UITableView
and add a UICollectionView
to your reusable tableView cell. UICollectionView
works similar to UITableView
in 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 UIScrollView
may 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 UITableViewCell
which has its own UITableView
but 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
回答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 Stack
s and ScrollView
alongside with List
(if you want):
在 swiftUI 中,您可以使用Stack
s 和ScrollView
with的强大功能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 List
for vertical data and a horizontal ScrollView
containing 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())
}
}
}