Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
Swift combine different type into one array to display data for UITableView

I have tableview where I need to show few sections. 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). Each of these sections contains cells which are songs names.

I have an array of songs called like this: var songsGroups = [SongGroup]. Which is actually my datasource.

SongGroup contains few properties:

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:

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. 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. Like in Objective-C as you remember. 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.

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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.
}
over 4 years ago · Santiago Trujillo Relatório

0

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
2) a table of songs, sectioned into groups.

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?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda