[英]Swift combine different type into one array to display data for UITableView
I have tableview where I need to show few sections. 我在tableview中需要显示几个部分。 You should imagine this table like a playlist of your songs.
您应该把这张表想象成歌曲的播放列表。 In the top first section I need to display a cell with button which will add more songs to the playlist and other sections of tableview are header titles of Music category (like pop, rock and etc).
在第一部分的顶部,我需要显示一个带有按钮的单元格,该单元格将向播放列表添加更多歌曲,而tableview的其他部分是Music类别的标题(如pop,rock等)。 Each of these sections contains cells which are songs names.
这些部分中的每个部分都包含作为歌曲名称的单元格。
I have an array of songs called like this: var songsGroups = [SongGroup]
. 我有一系列这样的歌曲:
var songsGroups = [SongGroup]
。 Which is actually my datasource. 实际上是我的数据源。
SongGroup
contains few properties: SongGroup
包含一些属性:
var categoryName: String
var songs: [Songs]
But the problem appears on the next level. 但是问题出现在下一级。 I every time need to check indexPath.section and do like this:
我每次都需要检查indexPath.section并这样做:
if indexPath.section == 0 {
// this is a section for ADD NEW SONG BUTTON cell no need in header title as there is no data repression only static text on the cell.
} else {
var musicCategoryName = songsGroups[indexPath.seciton - 1]. categoryName
headerTitle.title = musicCategoryName
}
As you see my code became magical by adding this cool -1
magical number. 如您所见,通过添加这个很酷的
-1
神奇数字,我的代码变得神奇了。 Which I replay don't love at all. 我重播了一点都不爱。
As an idea for sure I can try to combine my ADD NEW SONG BUTTON section (by adding some additional object) with songsGroups
array and create NSArray for this purposes. 可以肯定地说,我可以尝试将我的“添加新的
songsGroups
按钮”部分(通过添加一些其他对象)与songsGroups
数组组合在一起,并songsGroups
创建NSArray。 Like in Objective-C as you remember. 就像您记得的在Objective-C中一样。 So then my datasource array will looks like this:
因此,我的数据源数组将如下所示:
some NSArray = ["empty data for first cell", songsGroups[0], songsGroups[1]... etc]
So then there is no need to check any sections we can trust our array to build everything and even if I will add more empty data cells there is no need for me to handle my code via if block and adding tons of magical numbers. 这样一来,就无需检查我们可以信任数组的任何部分来构建所有内容,即使我将添加更多的空数据单元,也无需通过if块并添加大量的幻数来处理代码。
But the issue I see here that we don't use explicit types of array and it's upset. 但是我在这里看到的问题是我们不使用显式的数组类型,这很令人沮丧。
So maybe you know more beautiful solutions how to resolve my issue. 因此,也许您知道更多漂亮的解决方案来解决我的问题。
You can introduce a helper enum: 您可以介绍一个帮助程序枚举:
enum Section {
case empty
case songCategory(categoryName: String, songs: [String])
}
Your data source would then look something like this: 然后,您的数据源将如下所示:
let datasource: [Section] = [.empty, .songCategory(categoryName: "Category1", songs: ["Song 1", "Song2"])]
So now you can use pattern matching to fill the table view: 因此,现在您可以使用模式匹配来填充表格视图:
let section = datasource[indexPath.section]
if case let .songCategory(categoryName, songs) = section {
headerTitle.title = categoryName
} else {
// this is a section for ADD NEW SONG BUTTON cell no need in header title as there is no data repression only static text on the cell.
}
I am not sure if I understand you right. 我不确定我是否理解正确。 But is seems to me that you want to display
但是在我看来,您想展示
1) something that lets the user add a new song by tapping a button, and 1)允许用户通过点击按钮添加新歌曲的内容,以及
2) a table of songs, sectioned into groups. 2)一张歌曲表,分成几组。
If this is the case, why don't you put the add new song button in the table header view , and all your song groups and songs in a 2-dim array used as your dataSource? 如果是这种情况,为什么不将“添加新歌曲”按钮放在表标题视图中 ,并将所有歌曲组和2维数组中的歌曲用作数据源?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.