簡體   English   中英

如果未選擇tableView單元格時按下按鈕,則顯示警報消息

[英]Display alert message if button is pressed when no tableView cells selected

我有一個tableView,為單元格啟用了多個選擇。

TableViewController還包含帶有BarButtonItem的導航欄。 選擇此按鈕后,將打開另一個UITableViewController。

我想添加一個參數,如果沒有對UITableView進行選擇,則nextButton將觸發一條警告消息,提示用戶必須選擇一行或更多行。

怎么做這樣的事情?

當前,在didSelectRowAt方法中,我啟用了選中標記附件:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

定義一個變量,用於在控制器中存儲索引路徑:

var selectedIndexPaths = [IndexPath]()

每當用戶選擇一行時,將選定的indexPath添加到數組中:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedIndexPaths.append(indexPath)
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

檢查選擇的indexPaths是否為空,則什么都沒有選擇,您可以顯示警報:

if selectedIndexPaths.isEmpty {
    //Present alert
}

請注意,您沒有在原始問題中處理取消選擇 ,但流程類似。

定義將存儲所選IndexPath

var arrSelectedIndexPaths = [IndexPath]()

cellForRowAt內部,檢查數組是否包含當前的IndexPath 如果是,則將accessoryType .checkmark設置為.none否則設置為.none

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")
    cell.textLabel?.text = "Hello"

    if arrSelectedIndexPaths.contains(indexPath) {
        cell.accessoryType = .checkmark
    }
    else {
        cell.accessoryType = .none
    }

    return cell
}

didSelectRowAt內部,首先檢查您的數組是否包含選定的IndexPath 如果已經選擇,則取消選擇它,否則在數組中添加選擇的IndexPath

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    if arrSelectedIndexPaths.contains(indexPath) {
        if let index = arrSelectedIndexPaths.firstIndex(of: indexPath) {
            arrSelectedIndexPaths.remove(at: index)
        }
    }
    else {
        arrSelectedIndexPaths.append(indexPath)
    }

    self.tblVW.reloadData() //This will reload entire TableView
    //OR
    self.tblVW.reloadRows(at: [indexPath as IndexPath], with: .none) // This will reload only selected Row
}

您的Button動作代碼是這樣的。

@IBAction func YOUR_BUTTON_TRIGER_ACTION(_ sender: Any) {
    if arrSelectedIndexPaths.count <= 0 {
        //SHOW YOUR ALERT
    }
    else {
        //DO YOUR FURTHER TASK
    }
}

注意:不要在didSelectRowAt內寫tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark ,因為滾動TABLEVIEW時選擇消失或放錯了位置。

1.要檢查tableView是否為空,可以檢查用於填充tableViewarray / model是否為空。

假設arr是我們用作tableView dataSourcearray

var arr = [String]()

2.接下來,對於tableView選定的indexPaths ,您只需要檢查indexPathsForSelectedRows是否為空。

如果為空,則顯示UIAlertController ,否則根據您的需求導航到另一個UITableViewController

現在,結合以上所有條件, @IBActionnextButton就像這樣,

@IBAction func onTapNextButton(_ sender: UIBarButtonItem) {
    if arr.isEmpty || (tableView.indexPathsForSelectedRows != nil && !tableView.indexPathsForSelectedRows!.isEmpty) {
        //navigate to another UITableViewController
    } else {
        //Show Alert here...
        let alert = UIAlertController(title: "Alert..!!", message: "You must select atleast 1 row before proceeding.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }        
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM