0

I'm doing a project in Swift for my school, where I display data in a TableView. It shows information for two days, the actual day and the next day. Now I want to split the table in two Sections for each day. If it's possible with a section header with the date for each section. How can I do that, if I already have the TableView with the data. Best regards. ;)

    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {

    thisName = elementName

}

func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    if elementName == "element" {
        var myPlan = Plan()
        myPlan.klasse = planKlasse
        myPlan.raum = planRaum
        myPlan.vertreter = planVertreter
        myPlan.art = planArt
        myPlan.fach = planFach
        myPlan.stunde = planStunde
        myPlan.lehrer = planLehrer
        myPlan.datum = planDatum

        print(myPlan)
        tableViewDataSource.append(myPlan)


    }
}

func parser(_ parser: XMLParser, foundCharacters string: String) {


    let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

    if data.count != 0 {
        switch thisName {
        case "klasse": planKlasse = data
        case "vertreter": planVertreter = data
        case "raum": if data.hasPrefix("SN") { planRaum = "SNÜRN" } else {planRaum = data}
        case "art": planArt = data
        case "fach": planFach = data
        case "stunde": planStunde = data
        case "lehrer": planLehrer = data; if (data.isEmpty) { planLehrer = "-"}
        case "datum": planDatum = data
        default:
            break
        }

    }
}

func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
    print("failure error: ", parseError)
}







func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80
}


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

    if (searchController.isActive && searchController.searchBar.text != "") {
        return filteredElements.count
    } else {
        return tableViewDataSource.count
    }
}

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

    let myCell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath)

    var element: Plan

    if (searchController.isActive && searchController.searchBar.text != "") {
        element = filteredElements[indexPath.row]
        myCell.backgroundColor = UIColor.orange
    } else {
        element = tableViewDataSource[indexPath.row]
        myCell.backgroundColor = UIColor.white
    }

    let myKlasseLabel = myCell.viewWithTag(11) as! UILabel
    let myVertreterLabel = myCell.viewWithTag(12) as! UILabel
    let myRaumLabel = myCell.viewWithTag(13) as! UILabel
    let myArtLabel = myCell.viewWithTag(15) as! UILabel
    let myFachLabel = myCell.viewWithTag(14) as! UILabel
    let myStundeLabel = myCell.viewWithTag(16) as! UILabel
    let myLehrerLabel = myCell.viewWithTag(17) as! UILabel
    let myDatumLabel = myCell.viewWithTag(18) as! UILabel


    myKlasseLabel.text = element.klasse
    myVertreterLabel.text = element.vertreter
    myRaumLabel.text = element.raum
    myArtLabel.text = element.art
    myFachLabel.text = element.fach
    myStundeLabel.text = element.stunde + "."
    myLehrerLabel.text = "(" + element.lehrer + ")"
    myDatumLabel.text = element.datum


    if (element.art == "Klausur") {
        myFachLabel.text = "-"
        myLehrerLabel.text = "-"
    }

    if ((defaults.object(forKey: "klasseWahlDef")) as? String != nil) {

        if (tableViewDataSource[indexPath.row].klasse == (defaults.object(forKey: "klasseWahlDef")) as? String) {
        myCell.backgroundColor = UIColor.orange
        } else if (tableViewDataSource[indexPath.row].vertreter == (defaults.object(forKey: "LehrerDef")) as? String) {
           myCell.backgroundColor = UIColor.orange
        }
    }

    if tableViewDataSource[indexPath.row].datum != "hi" {

        let dateFormatterGet = DateFormatter()
        dateFormatterGet.dateFormat = "yyyy-MM-dd"

        let dateFormatterPrint = DateFormatter()
        dateFormatterPrint.locale = Locale(identifier: "de_DE")
        dateFormatterPrint.dateFormat = "E dd.MM.yyyy"


        if let date = dateFormatterGet.date(from: element.datum){
            //print(dateFormatterPrint.string(from: date))
            myDatumLabel.text = dateFormatterPrint.string(from: date)
        }
        else {
            print("There was an error decoding the string")
        }
    }
    return myCell

}
Kaitonee
  • 33
  • 5
  • 1
    What have you tried so far? Can you share us your code for the data source? – Rakesha Shastri Sep 10 '18 at 14:54
  • I would share the code, but it contains a lot of personal data of the school, thats the problem. I just have one table where I show data that I parsed from XML. Row for row it enters information in labels. – Kaitonee Sep 10 '18 at 15:09
  • I am not asking for school data. I’m asking for the code that you wrote. I don’t believe that’ll have any references to personal information. – Rakesha Shastri Sep 10 '18 at 15:10
  • I added the code at the top. Thats all for the parsing and the tableview – Kaitonee Sep 10 '18 at 15:16

3 Answers3

2

You have to define numbers of sections you want to display:

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

Then you have to define number of rows you want to display in every section:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        //return number of rows in first section
    }
    else {
        //return number of rows in second section
    }
}

If you want to create custom header, then you can use this function:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // return custom header view
}

However, if you want to only display text in a header, you can use the default one and use only titleForHeaderInSection function.

Of course, you also have to return proper view for cell. If you want to check the section number, you can simply call

indexPath.section
Mateusz
  • 233
  • 3
  • 8
0

Have you tried this func present in UITableViewDataSource:

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return yourDateString
}
user832
  • 796
  • 5
  • 18
  • I think I tried this, but I need to split the table for the two dates, like today and tomorrow. – Kaitonee Sep 10 '18 at 18:40
  • You need to split your tableViewDataSource into two parts on the bases of the dates, then in numberOfSections, just return 2, and in titleForHeaderInSection, check for section, if (section == 0), your title will be "Today" and similarly for (section == 1) your title will be "Tomorrow" and for numberOfRowsInSection return count according to your tableViewDataSource. – user832 Sep 12 '18 at 12:42
0

Try checking code - https://github.com/RockinGarg/Expandable-TableView-.git

Required Code

//setting number of Sections
func numberOfSections(in tableView: UITableView) -> Int {
    return section.count
}

If you want to customise headerView

//Setting Header Customised View
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    //Declare cell
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! TableViewCell

    //Setting Header Components
    headerCell.titleLabel.text = self.section[section]
    headerCell.ButtonToShowHide.tag = section

    //Handling Button Title
    if self.collapaseHandlerArray.contains(self.section[section]){
        //if its opened
        headerCell.ButtonToShowHide.setTitle("Hide", for: .normal)
    }
    else{
        //if closed
        headerCell.ButtonToShowHide.setTitle("Show", for: .normal)
    }

    //Adding a target to button
    headerCell.ButtonToShowHide.addTarget(self, action: #selector(ViewController.HandleheaderButton(sender:)), for: .touchUpInside)
        return headerCell.contentView

 }

Number Of Rows

//Setting number of rows in a section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.collapaseHandlerArray.contains(self.section[section]){
        return items[section].count
    }
    else{
        return 0
    }        
}
iOS Geek
  • 4,825
  • 1
  • 9
  • 30