2014-06-28 30 views
20

如果我取消如何在Swift中使用dequeueReusableCellWithIdentifier?

tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) 

我上寫着UITableView? does not have a member named 'dequeueReusableCellWithIdentifier'

如果我拆禮物的tableview那麼錯誤消失行

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 

得到一個錯誤,但在Objective-C,我們通常會檢查單元格是否存在,如果不存在,我們會創建一個新單元格。在Swift中,由於提供的樣板文件使用關鍵字let,並打開一個可選項,所以如果它爲零,我們不能重新分配它。

什麼是在Swift中使用dequeueReusableCellWithIdentifier的正確方法?

+0

。不用這種方法。 docs:「UITableViewCell對象與關聯的重用標識符。該方法總是返回一個有效的單元格」 –

+0

困惑的我也因爲我記得ObjC日子裏你檢查爲零的情況下,如果沒有細胞重用,並且如果零創建一個新的使用'reuseIdentifier'的單元格。我再次閱讀當前的Apple文檔,它說:「如果有現成的單元格可用,或者使用之前註冊過的類或nib文件創建一個新的單元格,則此方法會將其取出。」所以,現在這個功能對我們來說都很好,這很好。雖然看到這個討論,但有2個出列調用 - 一個返回可選,另一個不可以:[link](https://www.natashatherobot.com/ios-using-the-wrong-dequeuereusablecellwithidentifier/) –

回答

32

可以隱含解開的參數方法,並且還投dequeueReusableCellWithIdentifier結果給下面的代碼簡潔:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell 

    //configure your cell 

    return cell 
} 
+3

這也是我可以使用的答案 - 主要區別是在函數定義中使用'!'(用於隱式解包的可選)而不是'''(只是指出那些沒有立即注意的人區別)。我也認爲蘋果公司在XCode 6中提供的樣板文件也會替代它們的定義。 – JuJoDi

5

你需要解開了的tableView變量,因爲它是一個可選

if let realTableView = tableView { 
    let cell = realTableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 
    // etc 
} else { 
    // tableView was nil 
} 

,或者您可以通過

tableView?.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 

縮短它在對你關於「在Objective-C,我們會質疑通常檢查單元格是否存在,如果不存在,我們創建一個新單元格,dequeueReusableCellWithIdentifier總是返回一個單元格(只要你已經爲這個標識符註冊了一個類或筆尖),所以你不需要創造一個新的。

+0

dequeueReusableCellWithIdentifier並不總是返回一個單元格,cf文檔 – greg3z

+0

對不起,如果你設置正確,我的意思是總是返回一個單元格。我已經編輯它更清晰 – iain

+0

如果您在cellForAtIndexPath中使用此方法,您如何返回單元格? – senty

24

如果細胞類型尚未與表視圖表之前註冊加載後,你可以使用下面的獲取單元的實例:

private let cellReuseIdentifier: String = "yourCellReuseIdentifier" 

// MARK: UITableViewDataSource 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) 
    if (cell == nil) { 
    cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:cellReuseIdentifier) 
    } 
    cell!.textLabel!.text = "Hello World" 
    return cell! 
} 
+0

給出了迅疾2.0警告:從「的UITableViewCell」到「的UITableViewCell」'有條件投始終succeeds' –

+0

只需要刪除''因爲是 – Zorayr

+0

,我認爲if語句'如果(細胞==零){'? ...也應該被刪除。由於單元格不是可選類型,因此會出現錯誤。 –

2

SWIFT版本3:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath!) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier:"CellIdentifier", for: indexPath) as UITableViewCell 
     return cell 
    } 
1

在斯威夫特3和斯威夫特4版本你只需要使用有關的細胞可能是零這個

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath as IndexPath) as UITableViewCell 
     cell.textLabel?.text = "cell number \(indexPath.row)." 

     //cell code here 

     return cell 
    } 

在斯威夫特2版

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell 

cell.textLabel?.text = "cell number \(indexPath.row)." 

     //cell code here 

     return cell 
    } 
相關問題