创建表
self.automaticallyAdjustsScrollViewInsets = false;
tableView.frame = CGRect.init(x: 0, y: 64, width: self.view.frame.size.width, height: self.view.frame.size.height);
tableView.backgroundColor = UIColor.white;
self.view.addSubview(tableView);
tableView.delegate = self;
tableView.dataSource = self;
tableView.register(UINib.init(nibName: “WLTableViewCell”, bundle: nil), forCellReuseIdentifier: “WLTableViewCell”);
tableView在没有实现DataSource的三个方法是 tableView.dataSource = self; 会报错
//关于tableView的多选操作
**func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.init(rawValue: UITableViewCellEditingStyle.insert.rawValue | UITableViewCellEditingStyle.delete.rawValue)!
}**
**//多选选中是的方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//处理选中
}
//多选取消选中执行的方法
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
//处理取消选中
}**
关于tableView的右滑删除删除的操作
在tableView delegate 的editingStyleForRowAtindexPath 方法中返回
UITableViewCellEditingStyle.delete即可执行多选操作(删除操作有一个必须执行的方法,不执行 删除也不起作用)
删除必须执行的方法
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
print("");
}
如果上面的方法没有执行 侧滑删除无效
在下面的方法中 返回值可以修改删除按钮的文字
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "删除";
}
//tableView 侧滑多个按钮
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
var action1 = UITableViewRowAction();
var action2 = UITableViewRowAction();
var action3 = UITableViewRowAction();
action1 = UITableViewRowAction.init(style: UITableViewRowActionStyle.default, title:”置顶”, handler: { (UITableViewRowAction, IndexPath) in
//执行操作
});
action1.backgroundColor = UIColor.blue;
action2 = UITableViewRowAction.init(style: UITableViewRowActionStyle.default, title:”删除”, handler: { (UITableViewRowAction, IndexPath) in
//执行操作
});
action2.backgroundColor = UIColor.yellow;
action3 = UITableViewRowAction.init(style: UITableViewRowActionStyle.default, title:”隐藏”, handler: { (UITableViewRowAction, IndexPath) in
//执行操作
});
action3.backgroundColor = UIColor.red;
return [action1,action2,action3];
}