簡體   English   中英

willDisplayCell委托方法Swift

[英]willDisplayCell delegate method Swift

我一直在研究tableview並堅持以下問題。

據我所知, willDisplayCell委托方法應該允許我訪問當前的單元格設計。
我的問題是 - 我無法訪問自定義單元格的實例,因為它是在cellForRowAtIndexPath方法中單獨聲明的。 如果我再次聲明它 - 我無法訪問它的對象(我有一個uibutton )。 或者我可能誤解了這種方法的用法。

這是我的代碼

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

//declaring the cell variable again

    var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell

    if let text = cell.lblCarName.text where !text.isEmpty {
        cell.act1.hidden = false
    } else {
        println("Failed")
    }
}

我在代碼中看到的目標是訪問TblCell uilabel並對其應用條件 - 如果它的nil - 顯示uibutton

注意:我的委托和數據源設置正確,方法正在運行,應用程序編譯,但條件不起作用,因為我打算他們。

謝謝!

此委托為您提供現在將顯示的單元格的實例,並且實際將特定單元格作為cell參數發送。 Typecast它並使用條件。

請嘗試使用此代碼。

var cell:TblCell = cell as! TblCell

if let text = cell.lblCarName.text where !text.isEmpty {
    cell.act1.hidden = false
} else {
    println("Failed")
}

我認為更好的方法是:

guard let cell = cell as? TblCell else { return }

在willDisplayCell里面

暫無
暫無

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

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