答案 0 :(得分:1)
基本部分和行。
var titleString = [0 : "Bat and Ball", 1 : "Hockey"]
var rowString = [0: ["Baseball", "Softball", "Cricket"], 1: ["Field Hockey", "Ice Hockey", "Roller Hockey"]]
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (rowString[section]?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SOTableViewCell
cell.testLbl.text = rowString[indexPath.section]?[indexPath.row]
cell.selectionStyle = .none
return cell
}
// Uncomment for Default design and comment viewForHeaderInSection method
//func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//return titleString[section]
//}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 25))
sectionView.backgroundColor = UIColor.magenta
let sectionName = UILabel(frame: CGRect(x: 5, y: 0, width: tableView.frame.size.width, height: 25))
sectionName.text = titleString[section]
sectionName.textColor = UIColor.white
sectionName.font = UIFont.systemFont(ofSize: 14)
sectionName.textAlignment = .left
sectionView.addSubview(sectionName)
return sectionView
}