Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda