class LxrViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
var SectionArr:[String] = []
var rowName1:[String] = []
var rowName2:[String] = []
var rowName3:[String] = []
var rowName4:[String] = []
//分区
func numberOfSections(in tableView: UITableView) -> Int {
return SectionArr.count
}
//分行
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return rowName1.count
case 1:
return rowName2.count
case 2:
return rowName3.count
case 3:
return rowName4.count
default:
return 0
}
}
//行内容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “cell”)!
switch indexPath.section {
case 0:
cell.textLabel?.text = rowName1[indexPath.row]
case 1:
cell.textLabel?.text = rowName2[indexPath.row]
case 2:
cell.textLabel?.text = rowName3[indexPath.row]
case 3:
cell.textLabel?.text = rowName4[indexPath.row]
default:
“”
}
return cell
}
//组头
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let View = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
view.backgroundColor = UIColor.orange
let lable = UILabel(frame: CGRect(x: 10, y: 5, width: 100, height: 30))
lable.text = SectionArr[section]
View.addSubview(lable)
let btn = UIButton(frame: CGRect(x: 300, y: 5, width: 100, height: 30))
btn.setTitle("添加联系人", for: .normal)
btn.setTitleColor(UIColor.orange, for: .normal)
btn.addTarget(self, action: #selector(bbton), for: .touchUpInside)
View.addSubview(btn)
btn.tag = 100+section
return View
}
//删除单元格
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
rowName1.remove(at: indexPath.row)
case 1:
rowName2.remove(at: indexPath.row)
case 2:
rowName3.remove(at: indexPath.row)
case 3:
rowName4.remove(at: indexPath.row)
default:
""
}
self.table?.reloadData()
}
//表格
var table:UITableView?
override func viewDidLoad() {
super.viewDidLoad()
//标题
self.navigationItem.title = "联系人"
//右按钮
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add))
//表格
table = UITableView(frame: self.view.frame, style: .plain)
self.view.addSubview(table!)
table?.delegate = self
table?.dataSource = self
table!.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
//右按钮
@objc func add(){
let vc = AddViewController()
vc.jump = {
(str1) ->() in
if str1 == "" {
print("内容不能为空")
}else{
self.SectionArr.append(str1)
}
self.table?.reloadData()
}
self.navigationController?.pushViewController(vc, animated: true)
}
//组头右按钮
@objc func bbton(btn:UIButton){
let vc = CjdViewController()
vc.jumpScore = {
(str1,str2,str3) ->()in
switch btn.tag {
case 100:
self.rowName1.removeAll()
self.rowName1.append("名:\(str1)")
self.rowName1.append("名:\(str2)")
self.rowName1.append("名:\(str3)")
case 101:
self.rowName2.removeAll()
self.rowName2.append("名:\(str1)")
self.rowName2.append("名:\(str2)")
self.rowName2.append("名:\(str3)")
case 102:
self.rowName3.removeAll()
self.rowName3.append("名:\(str1)")
self.rowName3.append("名:\(str2)")
self.rowName3.append("名:\(str3)")
case 103:
self.rowName4.removeAll()
self.rowName4.append("名:\(str1)")
self.rowName4.append("名:\(str2)")
self.rowName4.append("名:\(str3)")
default:
""
}
self.table?.reloadData()
}
self.navigationController?.pushViewController(vc, animated: true)
}
}