class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
UITableView
let tableView = UITableView.init(frame: CGRect(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: SCREEN_WIDTH, height: SCREEN_HEIGHT)), style: .plain)
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView.init()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
self.view.addSubview(tableView)
UITableViewDelegate
// Cell 內(nèi)容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "mycell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "mycell")
}
cell?.textLabel?.text = dataSource[indexPath.row]
return cell!
}
// 自定義 頭視圖
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 44))
headerView.backgroundColor = UIColor.red
return headerView
}
// 自定義 尾視圖
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 100))
footerView.backgroundColor = UIColor.green
return footerView
}
// cell 將要顯示
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
// cell 已經(jīng)顯示
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}
// header 將要顯示
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
}
// header 已經(jīng)顯示
func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) {
}
// footer 將要顯示
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
}
// footer 已經(jīng)顯示
func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) {
}
// 設(shè)置cell 高
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
// 設(shè)置cell 高的估計(jì)值
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.0
}
// 設(shè)置header 高
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0
}
// 設(shè)置header 高的估計(jì)值
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 100.0
}
// 設(shè)置footer 高
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 44.0
}
// 設(shè)置footer 高的估計(jì)值
func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
return 100.0
}
// 設(shè)置cell 是否可以高亮
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return true
}
// cell 高亮?xí)r調(diào)用
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
}
// cell 取消高亮?xí)r調(diào)用
func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
}
// cell 將要選中
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
return indexPath
}
// cell 已經(jīng)選中
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
// cell 將要取消選中
func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? {
return indexPath
}
// cell 已經(jīng)取消選中
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
}
// 設(shè)置被編輯時(shí)的樣式(默認(rèn)刪除)
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .delete
}
// 自定義刪除按鈕
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "一鍵刪除"
}
// 設(shè)置編輯時(shí)按鈕
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let action1 = UITableViewRowAction.init(style: .normal, title: "喜歡") { (action:UITableViewRowAction, indexPath:IndexPath) in
//邏輯
}
let action2 = UITableViewRowAction.init(style: .normal, title: "不喜歡") { (action:UITableViewRowAction, indexPath:IndexPath) in
//邏輯
}
return [action1, action2]
}
// 編輯時(shí)背景是否縮進(jìn)
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return true
}
// 將要編輯
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
}
// 結(jié)束結(jié)束編輯
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
}
// 移動(dòng)行
func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
return proposedDestinationIndexPath
}
UITableViewDataSource
// 返回每個(gè)分組行數(shù)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
// 返回分組數(shù)(默認(rèn)為1)
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// 返回每個(gè)分區(qū)頭部標(biāo)題
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "頭"
}
// 返回每個(gè)分區(qū)尾部標(biāo)題
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "尾"
}
// 行是否可編輯
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
// 編輯調(diào)用
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
}
// 行是否可移動(dòng)
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
// 移動(dòng)調(diào)用
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
}
// 索引
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return ["0","1","2","3","4","5","6","7","8","9"]
}
// 設(shè)置索引對(duì)應(yīng)的分區(qū)
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return 0
}