//聲明
var selectPath: IndexPath! //單選
var cellIndexPath = NSMutableArray.init() //多選
//懶加載
lazy var tableView: UITableView = {
let tableView = UITableView.init(frame: self.view.bounds, style: .plain)
tableView.delegate = self;
tableView.dataSource = self
tableView.backgroundColor = UIColor.groupTableViewBackground
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none //去掉分割線
return tableView
}()
代理實現(xiàn)
單選
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellSting = "UITableViewCell" ;
var cell = tableView.dequeueReusableCell(withIdentifier: cellSting)
//(withIdentifier: cellSting, for: indexPath)
if cell == nil {
cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellSting)
}
cell?.textLabel?.text = "顯示\(indexPath.row)行";
//單選
if selectPath == indexPath {
cell?.accessoryType = .checkmark
}
else{
cell?.accessoryType = .none
}
return cell!;
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("點(diǎn)擊的第\(indexPath.row)行")
if selectPath == nil {
let cell = tableView.cellForRow(at: indexPath);
cell?.accessoryType = .checkmark
selectPath = indexPath
}
else
{
if selectPath == indexPath {
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .none
selectPath = nil
}
else
{
let cell1 = tableView.cellForRow(at: selectPath!)
cell1?.accessoryType = .none
selectPath = indexPath
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
}
}
}
多選
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellSting = "UITableViewCell" ;
var cell = tableView.dequeueReusableCell(withIdentifier: cellSting)
if cell == nil {
cell = UITableViewCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: cellSting)
}
cell?.textLabel?.text = "顯示\(indexPath.row)行";
//多選
if self.cellIndexPath.contains(indexPath) {
cell?.accessoryType = .checkmark
}
else
{
cell?.accessoryType = .none
}
return cell!;
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("點(diǎn)擊的第\(indexPath.row)行")
if self.cellIndexPath.contains(indexPath) {
self.cellIndexPath.remove(indexPath)
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .none
}
else
{
self.cellIndexPath.add(indexPath)
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
}
}
刪除
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return UITableViewCell.EditingStyle.delete;
}
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
print("==willBeginEditingRowAt========= \(indexPath.row)")
}
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
return "刪除"
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
print("點(diǎn)擊刪除按鈕===== \(indexPath.row)");
}
左滑顯示多個
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
var action1 = UITableViewRowAction();
var action2 = UITableViewRowAction();
var action3 = UITableViewRowAction();
action1 = UITableViewRowAction.init(style: .default, title: "置頂", handler: { (UITableViewRowAction, IndexPath) in
//執(zhí)行操作
print("置頂===========\(indexPath.row)")
});
action1.backgroundColor = UIColor.blue;
action2 = UITableViewRowAction.init(style: .destructive, title: "發(fā)現(xiàn)", handler: { (UITableViewRowAction, IndexPath) in
print("發(fā)現(xiàn)===========\(indexPath.row)")
});
action2.backgroundColor = UIColor.orange;
action3 = UITableViewRowAction.init(style: UITableViewRowAction.Style.default, title:"隱藏", handler: { (UITableViewRowAction, IndexPath) in
//執(zhí)行操作
print("隱藏===========\(indexPath.row)")
});
action3.backgroundColor = UIColor.red;
return [action1,action2,action3];
}