2017-06-12 19 views
1

私はMacOSアプリケーションの構築に取り組んでいます。私は追加ボタンを押すと、セルを更新するテーブルビューを作成しようとしています。識別子付きNSTableViewセルは無限にし続けます

enter image description here

後に私のコードです:(: "NameCell"、所有者:自己withIdentifier)として

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { 
    let identifier = tableColumn?.identifier as NSString? 
    if (identifier == "NameCell") 
    { 
     var result: NSTableCellView 
     let cell = tableView.make(withIdentifier: "NameCell", owner: self) as! NSTableCellView 
     cell.textField?.stringValue = self.data[row].setting! 
      return cell 

    } 
    else if (identifier == "SettingCell") 
    { 
     if let cell = tableView.make(withIdentifier: "SettingCell", owner: self) as? NSTableCellView { 
     cell.textField?.stringValue = self.data[row].setting! 
     return cell 
    } 
    } 
    return nil 
} 

しかし、行はセル= tableView.makeてみましょう!それは

致命的なエラーがnilを返すためNSTableCellViewが失敗し続けるです:NameCellは enter image description here からである

オプションの値をアンラップしながら、予想外にnilを見つけ誰もがこの問題を解決するための方法を見つける私を助けてくださいことはできますか?

+0

私は与えるべきである識別子がNSTableColumnないNSTableCellView – user3284302

+0

に対応していると思いますはい、私はそれらの識別子はNSTableColumnの識別子でなければならないと思います。 tableViewがnilでないことを確認する方法を説明してください(コンセントを見る)? – user3284302

+0

列ビューとセルビューの両方の識別子を変更しましたか? – Willeke

答えて

2

NSTableCellViewに "NameCell"と "Identifier"を設定する必要があります。列の識別子は永遠に変更されませんので、従うよう、あなたのコードを簡素化する必要があります:XCodeのInterface Builderで

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { 
    var result: NSTableCellView 
    let cell = tableView.make(withIdentifier: "NameCell", owner: self) as! NSTableCellView 
    cell.textField?.stringValue = self.data[row].setting! 

    return cell 
} 

参照設定: enter image description here

+0

私のために働いていましたが、 'make(withIdentifier:NSUserInterfaceItemIdentifier(rawValue:" NameCell ")、owner:self)' –