在iOS开发中使用最为平凡的控件要数UITableView了,在swift中,UITableView和OC的代理方法相同,功能也相同,废话少说,直接上代码!
//定义tableView
let tableView = UITableView()
tableView.frame = UIScreen.main.bounds
tableView.delegate = self
tableView.dataSource = self
//设置设置footView为空 、头视图为一张图片
tableView.tableFooterView = UIView.init()
tableView.tableHeaderView = imageHeaderView
view.addSubview(tableView)
用懒加载定义一个数组/属性定义一个数组当做数据源,懒加载定义UIImageViews当做headerView
var itemStringArr = ["企划部","软件部","咨询部","人事部"]
lazy var dataList:[String] =
{return["a","b","c","d","e","f"] }()
lazy var imageHeaderView:UIImageView = {var headerImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 200)) headerImageView.image = UIImage(named: "呵呵") return headerImageView }()
苹果官方推荐将系统代理方法写在extension中,类似OC的category
代理方法写在当中
extension WMViewController: UITableViewDelegate,UITableViewDataSource
{
}
具体方法如下
//MARK:设置有多少行
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return dataList.count
default:
return itemStringArr.count
}}
//MARK: 设置有2段
func numberOfSections(in tableView: UITableView) -> Int {return 2 }
//MARK:设置tableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let ID = "cell" var cell = tableView.dequeueReusableCell(withIdentifier: ID) if cell == nil { cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: ID) } if indexPath.section == 0 { cell?.textLabel?.text = dataList[indexPath.row] cell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator }else { cell?.textLabel?.text = itemStringArr[indexPath.row] cell?.detailTextLabel?.text = "待办事项" } return cell! }
//MARK:设置headerInSection高度
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10
}//MARK:设置footerInSecton的高度 func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 0.01 } //MARK:设置cell的高度 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { switch indexPath.section { case 0: return 45 default: return 60 } }
//MARK:选中cell时触发
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//点击cell的效果
tableView.deselectRow(at: indexPath, animated: false)print("点击\(indexPath.row)") } //MARK:取消选中cell时触发 func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { print("取消\(indexPath.row)") }
//MARK:允许编辑
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}//MARK:编辑:删除、插入、无操作 func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { if indexPath.row < 3 { return UITableViewCellEditingStyle.delete }else if indexPath.row == 0{ return UITableViewCellEditingStyle.none }else { return UITableViewCellEditingStyle.insert } } //MARK:点击删除cell时触发 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { print("删除第\(indexPath.row)行") }
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
if indexPath.section == 1 {
return true
}else
{
return false
}
}