uitableviewcontroler with titleForHeaderInsection

1

Hello my problem is the following I have several cells created, with their respective titles each, but when I try to add more iten to each cell I get error, or does not present the value in the cell this is an example of the app

The question is where this example calculation area is the cell to how I can add another cell within that area without affecting the others, I'm using the code

/
//  ViewControllerGeometry.swift
//  calculos2
//
//  Created by Liantony Pozo on 7/19/17.
//  Copyright © 2017 Liantony Pozo. All rights reserved.
//

import UIKit

class ViewControllerGeometry: UIViewController , UITableViewDelegate , UITableViewDataSource {

     var tableArray : [Problems] = []
     var tableArrayGeometryProblem : [Problemp] = []
     var tableArrayProblemsSecond : [ProblemsSecond] = []


     var segueIdentifiers = ["prueba" , "B", "C"]


    override func viewDidLoad() {
        super.viewDidLoad()

        let geometryproblem = Problems (name : "a")
        tableArray.append(geometryproblem)



        let geometryProblemSecond = ProblemsSecond(nameSecond: "b")
        tableArrayProblemsSecond.append(geometryProblemSecond)

        let perimeterProblem = Problemp(nameGeometryProblem: "c")
        tableArrayGeometryProblem.append(perimeterProblem)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        switch section {


        case 0:

            return tableArray.count

        case 1 :

            return tableArrayProblemsSecond.count

        case 2:
            return tableArrayGeometryProblem.count



        default:
           return 0
        }

    //return tableArrayGeometryProblem.count


}




    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellGeometryproblem")! as  UITableViewCell


        let geometryproblem = tableArray [indexPath.row]
        let perimeterProblemCalculation = tableArrayGeometryProblem [indexPath.row]
        let geometryProblemSecond = tableArrayProblemsSecond [indexPath.row]




        switch indexPath.section {



        case 0 :
            if indexPath.row == 0 {
                cell.textLabel?.text = geometryproblem.name
            }
        case 1 :


            if indexPath.row == 0 {
                cell.textLabel?.text = geometryProblemSecond.nameSecond

            }


        case 2 :

            if indexPath.row == 0 {
                cell.textLabel?.text = perimeterProblemCalculation.nameGeometryProblem

            }

        default:
            break

            }
        return cell


        }
      /*  switch indexPath.section {

     case 0:
            cell.textLabel?.text = geometryproblem.name
        case 1 :
            cell.textLabel?.text = perimeterProblemCalculation.nameGeometryProblem



        default:
            return cell
        }



    return cell
    }*/





        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: segueIdentifiers[indexPath.row + indexPath.section], sender: self)




            }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        var title = ""


        switch section {
        case 0:
            title = "Area calculation "
        case 1 :

            title = "Perimeter calculation "

        case 2 :

            title = "Sen theorem "


        case 3 :

            title = "Inside angles of the triangle "



        default:
            break
        }
        return title
    }

}

Sometimes only one value presents me in the cell, the other does not show me or I get an error in the code if someone could guide me in that problem.

    
asked by Liantony Pozo 25.07.2017 в 16:25
source

1 answer

2

I understand that what happens to you is that if for example you had this:

let geometryproblem = Problems (name : "a")
tableArray.append(geometryproblem)
let geometryproblem2 = Problems (name : "z")
tableArray.append(geometryproblem2)

Only the Problems ("a") would appear in the table. If so, it is because when you are going to paint the cell you ensure that the row is 0, and then paint the table, 0 if it is not do nothing. On the cellForRowAtIndex you have this:

switch indexPath.section {
    case 0 :
        if indexPath.row == 0 {
            cell.textLabel?.text = geometryproblem.name
        }
    case 1 :
    ......

You are saying that for section 0, if it is the first cell that is painted, otherwise it does not do anything. I should remove that if. Also, it would not be necessary for you to obtain the model of all the data, only obtain it from the one you need.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellGeometryproblem")! as  UITableViewCell

    switch indexPath.section {
    case 0 :
        let geometryproblem = tableArray [indexPath.row]
        cell.textLabel?.text = geometryproblem.name
    case 1 :
        let geometryProblemSecond = tableArrayProblemsSecond [indexPath.row]
        cell.textLabel?.text = geometryProblemSecond.nameSecond
    case 2 :
        let perimeterProblemCalculation = tableArrayGeometryProblem [indexPath.row]
        cell.textLabel?.text = perimeterProblemCalculation.nameGeometryProblem
    default:
        break
        }
    return cell
    }

Nothing is displayed in the last section because you have a break in the cellForRowAtIndex.

If this is not your problem, then I have not understood what is happening to you

    
answered by 25.07.2017 / 18:39
source