Swift 将 TableView 分成两部分
Swift Split TableView in two sections
我正在 Swift 为我的学校做一个项目,我在 TableView 中显示数据。它显示两天的信息,实际日期和第二天。现在我想每天将 table 分成两个部分。如果可能的话,可以在 header 部分加上每个部分的日期。
如果我已经拥有包含数据的 TableView,我该怎么做。
最好的祝福。 ;)
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
}
您是否尝试过 UITableViewDataSource 中的这个功能:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return yourDateString
}
您必须定义要显示的部分的数量:
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
然后您必须定义要在每个部分中显示的行数:
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
}
}
如果你想创建自定义header,那么你可以使用这个函数:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// return custom header view
}
但是,如果您只想在 header 中显示文本,您可以使用默认的并且只使用 titleForHeaderInSection 函数。
当然,您还必须return正确查看单元格。如果要查看节号,只需调用
indexPath.section
尝试检查代码 - https://github.com/RockinGarg/Expandable-TableView-.git
所需代码
//setting number of Sections
func numberOfSections(in tableView: UITableView) -> Int {
return section.count
}
如果要自定义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
}
行数
//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
}
}
我正在 Swift 为我的学校做一个项目,我在 TableView 中显示数据。它显示两天的信息,实际日期和第二天。现在我想每天将 table 分成两个部分。如果可能的话,可以在 header 部分加上每个部分的日期。 如果我已经拥有包含数据的 TableView,我该怎么做。 最好的祝福。 ;)
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
}
您是否尝试过 UITableViewDataSource 中的这个功能:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return yourDateString
}
您必须定义要显示的部分的数量:
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
然后您必须定义要在每个部分中显示的行数:
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
}
}
如果你想创建自定义header,那么你可以使用这个函数:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// return custom header view
}
但是,如果您只想在 header 中显示文本,您可以使用默认的并且只使用 titleForHeaderInSection 函数。
当然,您还必须return正确查看单元格。如果要查看节号,只需调用
indexPath.section
尝试检查代码 - https://github.com/RockinGarg/Expandable-TableView-.git
所需代码
//setting number of Sections
func numberOfSections(in tableView: UITableView) -> Int {
return section.count
}
如果要自定义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
}
行数
//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
}
}