TableView 参考 : http://mft.iteye.com/admin/blogs/2314641
class Do_bookmark: UIViewController,UITableViewDelegate,UITableViewDataSource {
var tb:UITableView!;
//var data:[[String:Any]]!;
override func viewDidLoad() {
super.viewDidLoad()
// set nav bar
self.title = "列表"
let rightBtn = UIBarButtonItem(title: "编辑", style: .plain, target: self, action: #selector(Do_bookmark.navRightBtn));
self.navigationItem.rightBarButtonItem = rightBtn
//
//let f:CTFrame = self.view.frame as! CTFrame;
tb = UITableView(frame: CGRect(x: 0, y: 0,width: alert_width, height: alert_height));
tb.delegate = self
tb.dataSource = self;
//进入编辑状态是否可以点击每行
tb.allowsSelectionDuringEditing = true
self.view.addSubview(tb);
}
func navRightBtn(){//根据Table 的编辑状态 显示 “编辑”还是“完成”
if(tb.isEditing == true){
tb.setEditing(false, animated: true);
tb.isEditing = false;
self.navigationItem.rightBarButtonItem?.title = "编辑";
}else{
tb.setEditing(true, animated: true)
tb.isEditing = true;
self.navigationItem.rightBarButtonItem?.title = "完成";
}
}
var data = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"]
//每行有几个区块
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
//多少行
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return data.count
}
//-------- 进入编辑模式 ---------
//设置cell 是否允许拖动排序
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
if(indexPath.row == 0){//如果是第一行就不允许拖动
return false
}else{
return true
}
}
//拖动排序 后 时 触发
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
//data.chan
print("move row at")
//data.exchangeObject(at: (sourceIndexPath as NSIndexPath).row, withObjectAt: (destinationIndexPath as NSIndexPath).row)
}
//进入编辑模式后每行的显示样式
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if(indexPath.row == 0){//如果是第一行就不显示删除
return UITableViewCellEditingStyle.none
}else{
return UITableViewCellEditingStyle.delete
}
}
//对应行是否需要缩进
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
if(indexPath.row == 0){//如果是第一行就不缩进
return false
}else{
return true
}
}
//增加左划显示删除,
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
//如果点击了删除
if(editingStyle == UITableViewCellEditingStyle.delete){
print("you do del")
// 修改数据源 必须 要 执行,否则 会出错
data.remove(at: indexPath.row);
//删除 表 视图 对应的行
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)
}
}
//\\========= 进入编辑模式 ===============
//点击每行执行
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if(indexPath.row == 0){
self.navigationController?.pushViewController(Do_history(), animated: true)
}
}
//设置分割线位移
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.separatorInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0);
}
// 开始往每行写内容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//---
let cell = BookMarkViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "diy_cell") as BookMarkViewCell;
//UITableViewCellAccessoryDisclosureIndicator;
cell.title.text = data[indexPath.row];
cell.logo.image = #imageLiteral(resourceName: "bookmark_icon")
//去除点击后的显示灰色背景效果
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell;
//====
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
//
}
}