1

我正在尝试(但无法)为UISearchController单元格定义自定义样式。运行下面的代码会导致EXC_BAD_INSTRUCTION该行出现错误cell.textlabel?.text。我在这里错过了一些完全明显的东西吗?

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
}
4

1 回答 1

1

You are dequeuing a cell with the identifier "Cell" no matter what. Instead, first check which table view you are in, then dequeue a different cell (with a different identifier) if needed.

于 2014-10-17T15:24:07.277 回答