簡體   English   中英

TableView單元格選擇事件未觸發

[英]TableView cell select event not triggering

我已經在XIB文件中創建了一個組件。 該文件包括2個組件

  1. 標簽
  2. 表格檢視

然后,我已鏈接並將其File's Owner類設置為SampleView 我已經將XIB文件view附加到SampleView.swift文件中,並且該文件的類中只有以下代碼:

@IBOutlet var view: UIView!

我現在創建了一個帶有協議UIViewControllerUITableViewDelegateUITableViewDataSource的控制器文件SampleController 我將以下代碼放在init()函數中以顯示自定義組件:

init() {
        super.init(nibName: nil, bundle: nil)

        modalPresentationStyle = UIModalPresentationStyle.Custom
        view.addSubview(SampleView())
}

我正在使用此SampleController以編程方式顯示為Modal

這些代碼確實顯示為Modular,顯示Label和TableView。 它還在TableView中填充數據。 問題是:

當我點擊表格中的單元格時,它不會在第一次嘗試時觸發事件。 當我點擊另一個單元格時,它將觸發上一個單元格事件。

知道為什么會這樣嗎?

以下是用於填充和處理單元水龍頭的2個函數:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("CELL")
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL")
        }
        cell!.textLabel?.text = sampleData[indexPath.row]["title"]
        return cell
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
       print("tapped")
}

該死! 我正在使用didDeselectRowAtIndexPath而不是didSelectRowAtIndexPath 這就是午夜之后編程時會發生的情況。

您需要使用其他方法來使單元出隊:

var cell = tableView.dequeueReusableCellWithIdentifier("CELL", forIndexPath: indexPath)

順便說一句,不需要檢查零單元格。

暫無
暫無

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

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