2015-12-10 19 views
6

Cercando di elaborare un paio di scene nella mia app, non riesco a far funzionare TableViews come dovrebbero.Errori con le viste della tabella statica

sto facendo un impostazioni come tableView in cui l'utente sceglie tra due possibili impostazioni. Ogni opzione porta a new tableView in cui l'utente deve selezionare una delle opzioni disponibili. Una volta che l'utente ne ha scelto uno, l'etichetta nella prima scena mostrerà il valore scelto (che è memorizzato anche come NSUserDefaults). Quindi, se non sbaglio, ciò potrebbe essere ottenuto con 3 tabelle statiche. Il seguente codice è come Im cercando di farlo:

impostazioni iniziali visualizzare regolatore:

class SettingsVC2: UITableViewController{ 
override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 2 
} 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if indexPath.section == 0{ 

     let cell = tableView.dequeueReusableCellWithIdentifier("office_cell", forIndexPath: indexPath) 
     cell.textLabel?.text = "Select your office" 
     return cell 
} 
    else { 
     let cell = tableView.dequeueReusableCellWithIdentifier("dept_cell", forIndexPath: indexPath) 
     cell.textLabel?.text = "Select your department" 
     return cell 
    } 

Una delle tableViews impostazioni:

class SettingsDepartmentTVC: UITableViewController{ 

let departamentos: [String] = ["Sales", "Pre-Sales", "General Administration", "Customer Service", "Profesional Services", "Regionales"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return departamentos.count 
} 
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 

} 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath) 
    cell.textLabel?.text = self.departamentos[indexPath.row] 
    return cell 

} 
} 

credo che tutte le origini dati e delegati hanno ragione agganciati e UIKit viene importato anche se non viene mostrato.

Erros Im ottenendo: fallimento

asserzione in - [UITableView dequeueReusableCellWithIdentifier: forIndexPath:]

terminazione app a causa di eccezione non identificata 'NSInternalInconsistencyException', la ragione: 'in grado di annullare l'accodamento una cella con identificatore office_cell - deve registrare un pennino o una classe per l'identificatore o collegare una cella prototipo in uno storyboard


questo è il metodo in cui provo a popolare il tableView:

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


    let cell = tableView.dequeueReusableCellWithIdentifier("CommentsRowTVC", forIndexPath: indexPath) as! CommentsRowTVC 

    let single_comment = self.AOS[indexPath.row] 

    cell.titulo_comentario?.text = single_comment.titulo_comment 
     cell.cuerpo_comentario?.text = single_comment.cuerpo_comment 
    cell.fecha_comentario?.text = single_comment.fecha_comment 
    return cell 
} 

class CommentsRowTVC: UITableViewCell { 


@IBOutlet weak var titulo_comentario: UILabel! 
@IBOutlet weak var cuerpo_comentario: UILabel! 
@IBOutlet weak var fecha_comentario: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

}

Ecco l'ispettore connessioni per l'UIViewController in cui la tabella è:

Outlets

E questo per UIViewCellController:

Outlets2

errori a questo punto:

UIView tableView: numberOfRowsInSection: selettore non riconosciuto inviato ad esempio

terminazione app causa eccezione non identificata NSInvalidArgumentException, motivo: [UIView tableView: numberOfRowsInSection:] : selettore non riconosciuto inviato all'istanza

+0

è possibile aggiungere uno storyboard screenshot? – adolfosrs

+0

hai aggiunto l'indentificatore allo storyboard? – adolfosrs

+0

@PabloDuque dovresti impostare un punto di interruzione e provare a passare attraverso il tuo codice, vedere dove si interrompe e riportare –

risposta

2

Se stai davvero facendo celle di visualizzazione tabella statiche quindi è sufficiente eliminare questi metodi. Li usi solo quando stai rimuovendo dinamicamente le celle.

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return departamentos.count 
} 
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 

} 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath) 
    cell.textLabel?.text = self.departamentos[indexPath.row] 
    return cell 

} 

se si sta in realtà cercando di fare cellule dinamiche poi, come suggerisce il messaggio di errore, è necessario assicurarsi che il proprio identificativo partite quello che hai nel costruttore di interfaccia:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("{your_identifier}", forIndexPath: indexPath) 
    cell.textLabel?.text = self.departamentos[indexPath.row] 
    return cell 

} 
+0

Allora come aggiungo il contenuto alle celle? –

+1

Se stai creando celle statiche, lo fai nello storyboard. Basta trascinare le etichette e nominarle come desideri. "Statico" significa che non cambiano mai, quindi basta trascinare le etichette per eseguire il trucco ma solo se rimuovi i metodi che ho menzionato. –

+0

Va bene, vedo. Per quanto riguarda la vista tabella quali etichette mostrano cosa è memorizzato in NSUserDefautls? Sarebbe qualcosa di simile a quello che stavo cercando di fare funzionare? –

1

Con il il codice che hai fornito sembra che tu non abbia registrato le celle di visualizzazione tabella e gli identificatori con la vista tabella.

UITableView Class Reference

Discussione Prima di dequeueing tutte le cellule, chiamare questo metodo o la registerNib: forCellReuseIdentifier: metodo per dire la visualizzazione della tabella come per creare nuove cellule. Se una cella del tipo specificato non è attualmente in una coda di riutilizzo, la vista tabella utilizza le informazioni fornite per crea automaticamente un nuovo oggetto cella.

Se in precedenza è stato registrato un file di classe o pennino con lo stesso identificativo di riutilizzo , la classe specificata nel parametro cellClass sostituisce la voce precedente. È possibile specificare nil per cellClass se si desidera che annulli la registrazione della classe dall'identificatore di riutilizzo specificato.

Suggerisco di registrarli in viewDidLoad().

let KDepartmentCellIdentifier = "department_cell" 
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kKDepartmentCellIdentifier) 

    let KOfficeCellIdentifier = "office_cell" 
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kOfficeCellIdentifier) 

Se si utilizza un pennino quindi utilizzare

registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)