jest następująca realizacja cellForRowAtIndexPath technicznie poprawny sposób najlepszych praktyk z uwzględnieniem rozpakowaniu optionalsPrawidłowa realizacja cellForRowAtIndexPath w Swift 2.0
class MyTableViewController: UITableViewController {
var cell : UITableViewCell?
// other methods here
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell = tableView.dequeueReusableCellWithIdentifier("ItemCell")! as UITableViewCell
let myItem = items[indexPath.row]
cell!.textLabel?.text = myItem.name
cell!.detailTextLabel?.text = myItem.addedByUser
return cell!
}
}
Nie ma powodu, aby mieć opcjonalną zmienną komórki poza metodą, po prostu wprowadź nie-opcjonalną zmienną lokalną w metodzie – dan
Wskazówka: Utwórz nową aplikację Xcode za pomocą szablonu "Master-Detail Application", a otrzymasz próbkę kod za darmo. –