Danial Kosarifa
Danial Kosarifa

Reputation: 1074

why can I only present my first section in TableView Swift ?

I have created two arrays called section one and two to feed in their data to my table.

let Sec1 = ["Section" , "One"]
let Sec2 = ["Section", "Two"]

And I have create an array called sectionTitles which contains the name of my sections.

let sectionTitles = ["Section1" , "Sectoin2"]

And then I have the following delegate table view functions :

public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
    return sectionTitles[section]

    }
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        switch section {
        case 0 :
            return Sec1.count
        default:
            return Sec2.count

        }

    }
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = challengeTable.dequeueReusableCell(withIdentifier: cellID) as! CustomisedCell
        cell.selectionStyle = .none

        switch indexPath.section {
        case 0:
            cell.CAChallengeTitle.text = Sec1[indexPath.row]
            cell.CAChallengeDescription.text = "\(Sec1[indexPath.row])  "
            cell.CAChallengeButt.tag = indexPath[1]
            cell.CAChallengeButt.setTitle("I am interested >", for: .normal)
            print("Done with sectoin 0")
        default:
            cell.CAChallengeTitle.text = Sec2[indexPath.row]
            cell.CAChallengeDescription.text = "\(Sec2[indexPath.row])  "
            cell.CAChallengeButt.tag = indexPath[1]
            cell.CAChallengeButt.setTitle("I am interested >", for: .normal)
            print("Done with sectoin 1")

        }
       return cell

    }

    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        let height : CGFloat  = 60
        return height
    }

But in the end I only get to present the fist section only. Any idea where am I making my mistake ? enter image description here

Upvotes: 1

Views: 95

Answers (2)

Ajay saini
Ajay saini

Reputation: 2460

You have to implement numberOfRowsInSection method of tableView in case you are using only static sactions then you have to return the count of sections in numberOfRowsInSection method. otherwise, store all the sections in an array and return Array.count

Upvotes: 0

rmaddy
rmaddy

Reputation: 318774

You haven't implemented the numberOfSections data source method.

func numberOfSections(in tableView: UITableView) -> Int {
    return sectionTitles.count
}

Without this, the table assumes 1 section.

Upvotes: 4

Related Questions