2017-09-02 4 views
0

J'ai le code suivant jusqu'ici.Sections dans UITableView avec des cellules personnalisées

var someData = [SomeData]() 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.row == 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1 

     return cell 

    } else { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? Cell2 
     let someData = [indexPath.row] 
     //Set up labels etc. 


     return cell! 
    } 
} 

J'ai besoin Cell1 qui est une cellule statique et restera toujours à indexPath 0 être dans une section intitulée « Section1 », par exemple & tous être dans une section de l'Cell2 appelé « Section2 »

Autre DataSource & Méthodes Delegate;

func numberOfSections(in tableView: UITableView) -> Int { 
    return 2 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0 { 
     return 1 
    } else { 
     return someData.count 
    } 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if section == 0 { 
     return "Section1" } 
    else { 
     return "Section2" 
    } 
} 

Cela me renvoie tout ce qu'il faut pour la première section, toutefois, en ce qui concerne la deuxième partie (à cause du code à l'intérieur cellForRowAtIndex quelque part) l'article 2 contient Cell2 à 0. indexPath

Toute aide très appréciée.

+1

Dans l'enregistrement '' de cellForRowAtIndexPath' pour indexPath.section' au lieu de 'indexPath.row' – user1046037

+0

merci, @ user1046037 avait un comportement inhabituel sur l'index 0 de la deuxième section, mais j'ai réalisé que j'avais besoin de me référer à la section ainsi que sur ma taille pour la méthode indexPath. S'il vous plaît ajouter comme une réponse et mal l'accepter. –

Répondre

1

cause racine:

En cellForRowAtIndexPath chèque indexPath.section au lieu de indexPath.row

Fix:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.section == 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1 

     return cell 

    } else { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? Cell2 
     let someData = [indexPath.row] 
     //Set up labels etc. 


     return cell! 
    } 
}