UITableView入门操作
1.基础操作
1.0 初始化UITableView
// 懒加载
lazy var tableView : UITableView = {
let tableView = UITableView(frame: view.bounds, style: .grouped)
tableView.delegate = self
tableView.dataSource = self
return tableView
}()
let tableView = UITableView(frame: view.bounds, style: .grouped)
tableView.dataSource = self
view.addSubview(tableView)
滚动到底部
使用scrollToRow方法滚动到最后一行
let secon = 1 //最后一个分组的索引(0开始,如果没有分组则为0)
let rows = 10 //最后一个分组最后一条项目的索引
let indexPath = IndexPath(row: rows, section: secon)
self.tableView?.scrollToRow(at: indexPath, at:.bottom, animated: true)
使用setContentOffset设置偏移量实现滚动:
let offset = CGPoint(x:0, y:self.tableView!.contentSize.height
- self.tableView!.bounds.size.height)
self.tableView!.setContentOffset(offset, animated: true)
1.1 协议方法
1.1.0 协议 - UITableViewDelegate
-
tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
:
设置单元格高度,每当表格需要显示时,都会调用此方法。 -
tableView(_ tableView: UITableView, heightForHeaderInSection section: Int)
:
设置某一索引下的章节头部的高度。 -
tableView(_ tableView: UITableView, heightForFooterInSection section: Int)
:
设置某一索引下的章节尾部的高度。 -
tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
:
当指定索引位置上的单元格即将显示时,调用此方法。此方法是委托对象有机会在单元格显示之前重写其状态属性,如背景颜色等。 -
tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
:
当用户点击选择指定索引位置的单元格时,调用此方法。 -
tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
:
当用户点击一个已经被选中的单元格时,调用此方法。
// 方式一
extension LZTableVC : UITableViewDelegate {
// 当用户点击选择指定索引位置的单元格时,调用此方法。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
}
// 设置单元格高度,每当表格需要显示时,都会调用此方法。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 74.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
}
1.1.1 协议 - UITableViewDataSource
-
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
:
初始化和复用指定索引位置的UITableViewCell,必须实现。 -
tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)
:
设置某一章节(section
)中的单元格数量,必须实现。 -
numberOfSections(in tableView: UITableView)
:
设置表格中的章节(section
)个数。 -
tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
:
设置指定章节的标题文字,如果不设置或代理返回值为nil,不显示。 -
tableView(_ tableView: UITableView, titleForFooterInSection section: Int)
:
设置章节脚部标题文字,如果不设置或代理返回值为nil,不显示。 -
tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath)
:
设置表格中指定索引位置的cell
是否可编辑,可编辑的cell
会显示插入和删除的图标。 -
tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
:
当完成插入或删除操作时会调用此方法。 -
tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath)
:
设置指定索引位置的cell
是否可以通过拖动的方式,改变它的位置。 -
tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
:
当cell
从一个位置拖动到另一个位置时调用此方法。
方式一:
extension LZTableVC : UITableViewDataSource {
// 设置表格中的章节(section)个数。
func numberOfSections(in tableView: UITableView) -> Int {
return dataSource.count
}
// 设置某一章节(section)中的单元格数量,必须实现。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// 初始化和复用指定索引位置的UITableViewCell,必须实现。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellID = "testCellID"
var cell = tableView.dequeueReusableCell(withIdentifier:cellID)
if cell == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellID)
}
cell?.textLabel?.text = "这个是标题"
cell?.detailTextLabel?.text = "这里是内容了油"
cell?.imageView?.image = UIImage(named: "icon")
return cell!
}
}
1.2 UITableViewCell基本操作
1.2.0 基本Cell设置
//MARK:初始化Cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "indexsCellId")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "indexsCellId")
}
let arr = contents[keys[indexPath.section]]
cell?.textLabel?.text = arr?[indexPath.row]
return cell!
}
1.2.1 Cell自定义
import UIKit
class AutoUITableViewCell: UITableViewCell {
let width:CGFloat = UIScreen.main.bounds.width
var userLabel:UILabel! // 名字
var brirthdayLabel:UILabel!//出事日期
var sexLabel:UILabel! //性别
var iconImv:UIImageView! //头像
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// 头像
iconImv = UIImageView(frame: CGRect(x: 20, y: 15, width: 44, height: 44))
iconImv.layer.masksToBounds = true
iconImv.layer.cornerRadius = 22.0
// 名字
userLabel = UILabel(frame: CGRect(x: 74, y: 18, width: 70, height: 15))
userLabel.textColor = UIColor.black
userLabel.font = UIFont.systemFont(ofSize: 15)
userLabel.textAlignment = .left
// 性别
sexLabel = UILabel(frame: CGRect(x: 150, y: 20, width: 50, height: 13))
sexLabel.textColor = .black
sexLabel.font = UIFont.systemFont(ofSize: 13)
sexLabel.textAlignment = .left
//出生日期
brirthdayLabel = UILabel(frame: CGRect(x: 74, y: 49, width: width-94, height: 13))
brirthdayLabel.textColor = .gray
brirthdayLabel.font = UIFont.systemFont(ofSize: 13)
brirthdayLabel.textAlignment = .left
contentView.addSubview(iconImv)
contentView.addSubview(userLabel)
contentView.addSubview(sexLabel)
contentView.addSubview(brirthdayLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
网友评论