2014-10-17 35 views
1

我正在嘗試(但無法)爲UISearchController單元格定義自定義樣式。運行下面的代碼會導致cell.textlabel?.text行上出現EXC_BAD_INSTRUCTION錯誤。我錯過了一些完全明顯的東西嗎?用於UISearchController的自定義單元格

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell! 
    if (cell != nil) { 
     cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell") 
    } 
    if (tableView == self.searchDisplayController?.searchResultsTableView) { 
     let data: AnyObject = self.searchResults.objectAtIndex(indexPath.row) as AnyObject 
     println("searchresults: \(data)") 
     cell.textLabel?.text = data.valueForKeyPath("name") as? String 
     cell.detailTextLabel?.text = data.valueForKeyPath("placemark") as? String 
    } 
    return cell 
} 
+0

你檢查過密鑰「name」的值是不是零? – DaSilva 2014-10-17 13:44:19

+0

數據中對象的類型是什麼?它是一個快速類型還是Obj-C類型? – 2014-10-17 14:12:29

+0

順便說一下,第5行應該是'if cell == nil {'而不是if(cell!= nil){'。 – 2014-10-17 14:22:01

回答

1

無論發生什麼情況,您都將出現帶有標識符「單元格」的單元格。相反,首先檢查您所在的表視圖,然後根據需要出列不同的單元格(使用不同的標識符)。

+0

這工作就像一個魅力。謝謝!包裝我的代碼 if(tableView == self.searchDisplayController?.searchResultsTableView) 解決了這個問題。 – UncleAlfonzo 2014-10-18 04:54:28