/*
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellID)
需要和写在cellForRow里面的方法
tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
配对使用
*/
import UIKit
class TestViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
var tableView:UITableView!
var dataArray = NSMutableArray()
let cellID = "testCellID"
var isEdit = false // 判断tableview是否在编辑状态
override func viewDidLoad() {
super.viewDidLoad()
self.title = "UITableView"
// 数据源数组
self.dataArray = ["UIScrollView",
"Xib自适应cell高度",
"UICollectionView",
"UIDatePicker",
"UIAnimationViewController",
"代码自定义cell",
"XGBlockValueTest",
"自定义View测试"
]
// self.dataArray.addObject("heiehi")
// 添加nav右侧按钮
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "编辑", style: UIBarButtonItemStyle.Done, target: self, action:Selector("rightBarButtonItemClicked"))
// 初始化UITableView
self.tableView = UITableView(frame: CGRectMake(0.0, 0.0, KLScreenWidth, KLScreenHeight), style: .Plain)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.rowHeight = 60.0
self.tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellID)// cell的第二种写法 注册cell
self.view.addSubview(self.tableView)
// 去掉没有数据的cell的分割线
self.tableView.tableFooterView = UIView()
}
// nav添加右按钮
func rightBarButtonItemClicked(){
if isEdit {
self.tableView.setEditing(false, animated: true)
isEdit = false
}else{
self.tableView.setEditing(true, animated: true)
isEdit = true
}
}
// cell内容的显示
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
/*
//cell的写法1:
var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellID)
}
cell!.textLabel!.text = self.dataArray[indexPath.row] as? String
return cell!
*/
// cell的写法2:
//
let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = self.dataArray[indexPath.row] as? String
return cell
}
// 返回的cell的行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataArray.count
}
// cell选中时的状态
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// 设置返回的cell的高度
// func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// return 60.0
// }
// 可以被编辑
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
// 确定编辑模式(默认是滑动删除模式)
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
// 设置编辑模式为删除
return UITableViewCellEditingStyle.Delete
}
// 具体编辑操作(默认删除操作)
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// 先移除数据源数据
self.dataArray.removeObjectAtIndex(indexPath.row)
// 再动态刷新UITableView
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
}
// 允许移动某行(排序)
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
// 实现排序
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
// 从数组中读取需要移动行的数据
let object:AnyObject = self.dataArray[sourceIndexPath.row]
// 在数组中先移除需要移动行的数据
self.dataArray.removeObjectAtIndex(sourceIndexPath.row)
// 把需要移动的cell数据插到到想要移动的数据前面
self.dataArray.insertObject(object, atIndex: destinationIndexPath.row)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
swift里UITableView简单的用法
最新推荐文章于 2024-04-25 09:57:21 发布