UITableView
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let screenRect = UIScreen.main.bounds
let tableRect = CGRect(x: 0, y: 20, width: screenRect.size.width, height: screenRect.size.height)
let tableView = UITableView(frame: tableRect, style: .grouped) // .plain为默认风格
tableView.isEditing = true
tableView.setEditing(true, animated: true)
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
}
// 设置每个分区cell数量
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 3
} else {
return 10
}
}
// 设置分区数量
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
// 设置行高
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 100
} else {
return 44
}
}
// 设置cell尾视图高度
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 50
}
// 设置cell头视图高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
// 设置分区头视图
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
view.backgroundColor = UIColor.blue
return view
}
// 设置分区尾视图
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
view.backgroundColor = UIColor.green
return view
}
// cell复用
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "reusedCell"
var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
}
cell?.textLabel?.text = "第\(indexPath.section)分区"
cell?.detailTextLabel?.text = "第\(indexPath.row)行"
cell?.imageView?.image = UIImage(named: "image")
cell?.backgroundColor = UIColor.lightGray
cell?.accessoryType = .checkmark
/*
case none // 无附加视图
case disclosureIndicator // 规则的前进样式尖头
case detailDisclosureButton // 复合样式
case checkmark // 对号图标样式
case detailButton // 详情按钮样式
*/
return cell!
}
// cell选中后操作
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
// cell编辑模式,插入 删除
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
if indexPath.section == 0 {
return .insert
} else {
return .delete
}
}
//设置某行cell是否支持移动
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
// 设置移动后回掉方法
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
}
// 设置删除按钮标题
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "删除"
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "添加"
}
// // 提交编辑操作时候触发的方法
// func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
//
// if editingStyle == .delete && indexPath.section == 1 {
// tableView.deleteRows(at: [indexPath], with: .right)
// }
//
// if editingStyle == .insert && indexPath.section == 0{
// tableView.insertRows(at: [indexPath], with: .right)
// }
// }
//
//添加索引栏
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return ["one","two"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
网友评论