簡體   English   中英

Swift UiTableViewCell長按后重置

[英]Swift UiTableViewCell Resets after long press

在我的主視圖控制器中,我有一個按鈕,彈出一個帶有兩個按鈕和一個tableView的對話框。 tableView使用自定義UIView顯示,UITableViewCell也是自定義的。 UITableViewCell包含一個自定義復選框和一個UILabel。我正在嘗試向tableView添加一個輕擊手勢,這樣當我點擊一行時它會標記復選框。 這個功能有點可行,但是當我按下3秒以上時,UITableViewCell會重置為此

UITableViewCell錯誤Scre​​enShot

我不知道是什么造成了這個錯誤。 任何幫助,將不勝感激。

這是我的ViewController中的代碼,它打開了彈出對話框:

func locationButtonPressed(sender: UIBarButtonItem) {
    // Create a custom view controller
    let vc = RadiusViewController(nibName: "RadiusViewController", bundle: nil)
    // Create the dialog
    let popup = PopupDialog(viewController: vc, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)

    // create first button
    let cancelButton = CancelButton(title: "Cancel", height: 60, dismissOnTap: true) {
        print("Popup Canceled")
    }

    // create second button
    let okButton = DefaultButton(title: "Ok", height: 60, dismissOnTap: true) {
        print("Ok button pressed")
    }

    // add buttons to dialog
    popup.addButtons([cancelButton, okButton])

    // present dialog
    self.present(popup, animated: true, completion: nil)

    print("location button pressed")
}

使用tableView在我的自定義UIView中點擊手勢功能:

override func viewDidLoad() {
    super.viewDidLoad()

    ...code

    let tap = UITapGestureRecognizer(target: self, action: #selector(tableTapped))
    self.tableView.addGestureRecognizer(tap)
}

func tableTapped(tap:UITapGestureRecognizer) {
    let location = tap.location(in: self.tableView)
    let path = self.tableView.indexPathForRow(at: location)
    if let indexPathForRow = path {
        self.tableView(self.tableView, didSelectRowAt: indexPathForRow)
        print("Tapped on the table")
    } else {
        // handle tap on empty space below existing rows however you want
    }
}

我想你可能需要在tableviewcell上添加點擊手勢,而不是tableview。

您只需要讓ViewController實現UITableViewDelegate。 didSelectRowAtIndexPath有一個回調方法,您可以在其中設置邏輯以訪問單元格的屬性,例如自定義復選框。 不需要輕擊手勢識別器,因為這是本機提供的功能。

UITableViewDelegate文檔

暫無
暫無

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

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