簡體   English   中英

帶有tableview的popover控制器

[英]popover controller with tableview in swift

我有一個帶有條形按鈕項的TableView。 當我單擊條形按鈕時,將顯示PopOver菜單。 當我單擊Popover菜單項時,請執行一些操作(例如帶有特定項的tableview數據),我想將PopOver菜單用作“過濾器”類型:

這是代碼:

查看控制器代碼

@IBAction func ButtonClick(_ sender: UIBarButtonItem) {
     self.performSegue(withIdentifier: "POP1", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier ==  "POP1"            
    {
        let dest = segue.destination

        if let pop =  dest.popoverPresentationController
        {                
            pop.delegate = self
        }
    }
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

 func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return fruits.count
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    let fruitName = fruits[indexPath.row]
    cell.textLabel?.text = fruitName

    return cell
}

PopOVER代碼:

覆蓋func numberOfSections(在tableView中:UITableView)-> Int {

    return 1

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return data.count

}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "POP", for: indexPath)

    cell.textLabel?.text = data[indexPath.row]
    return cell
}


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    var selectedItem = indexPath
    print (selectedItem.row)

}

在此處輸入圖片說明

當我在Popover菜單中單擊一個單詞時,它應該顯示在視圖控制器表視圖上顯示的每個字母開頭的單詞計數(A中的1個字母,B中的2個字母)。

您可以使用完成關閉或委托方法來實現您的目標。 這是一個有用的庫,它使用帶有通用數據類型的完成閉包來讓您自由地獲取/設置表視圖數據源作為鍵值元組。

對於委派,您可以執行以下操作:

protocol PopoverViewControllerDelegate: class {
    func didSelectData(_ result: String)
}

class PopoverViewController: UIViewController {
    weak var delegate: PopoverViewControllerDelegate?

    //...

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dismiss(animated: true, completion: nil)
        let selectedItem = arrayData[indexPath.row]
        delegate?.didSelectData(selectedItem)
    }
}

然后在當前的視圖控制器中:

class ViewController: UIViewController, PopoverViewControllerDelegate {

    // ...

    func didSelectData(_ result: String) {
        // Update fruits array
        tableView.reload()
    }
}

暫無
暫無

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

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