swift UITableView 單選多選 編輯

//聲明

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];
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容