在IOS开发中,UItableView 的使用真的是最常见最普通的了,现在在自学swift 今天也是这用Swift 写了写 UItableview的使用,还有一些经常出错的地方。下面我先把整个控制器的代理列出来,大家可以顺便看看 swift 是怎样遵守协议的。
import UIKit
// 遵守两个协议
class HomeViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor=UIColor.whiteColor()
// 创建tableview
creatTableview()
// Do any additional setup after loading the view.
}
// 创建tableView的方法
func creatTableview()
{
let tableview:UITableView = UITableView(frame: self.view.frame, style: UITableViewStyle.Plain)
tableview.delegate = self
tableview.dataSource = self
// 这里要不注册,用下面的 dequeueReusableCellWithIdentifier 这个方法的时候会崩溃,这里和大家说一下,dequeueReusableCellWithIdentifier("SwiftCell", forIndexPath: indexPath) 要使用这个方法,在这里你就必须得先注册一个复用的cell,然后使用的时候 dequeueReusableCellWithIdentifier 会去调用能复用的cell
tableview.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "SwiftCell")
self.view.addSubview(tableview)
}
// 组数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}
// 每组的个数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section
{
case 0:
return 5
case 1:
return 3
case 2:
return 4
default:
return 0
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("SwiftCell", forIndexPath: indexPath)
cell.textLabel?.text="你真的很帅"
return cell
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
// tableView是否能编辑,true 返回,就是能编辑
func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
// 你要删除cell的时候,这个方法会自己调用的,这里一般就是在数据源里面删除cell 对应的数据。
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
print("删了这条数据了")
}
// 这里是编辑的类型
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
}
// 这里是给 删除按钮上的 按钮文字进行重命名,这里是改成“别删呀”的名字。
func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
return "别删呀"
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
特别说明几点内容:
1.自定义cell时,
若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib 不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
2.需不需要注册? 使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell; 使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有