Ho un UITableView che ho creato in un UIStoryboard che ha due dinamico Prototipo UITableViewCells:iOS Swift - UITableViewCell personalizzato sottoclasse non la visualizzazione di contenuti
La schermata vi mostrerà che ho il primo Lo stile di UITableViewCell è impostato su Sottotitoli e il secondo è impostato su personalizzato con un'etichetta "Tocca per aggiungere" al centro. Il primo ha un identificatore di "Cell" e il secondo "AddCell". Ho creato un UITableViewController (ho anche provato un UITableView in un UIViewController), UITableViewCell sottoclasse in Swift e ho collegato tutti i miei punti vendita. Tuttavia, quando eseguo il simulatore, la cella viene caricata ed è toccabile, ma non sono stato in grado di farlo visualizzare alcun contenuto. (Ho provato ad aggiungere altri controlli, ma niente apparirà quando viene caricato il cellulare L'unica cosa che posso cambiare è backgroundColor del contentView..)
Ho il seguente codice Swift per l'UITableViewController:
import UIKit
class ListTableViewController: UITableViewController {
var listObjects: ListObject[] = DataManager.instance.allListObjects() as ListObject[]
init(style: UITableViewStyle) {
super.init(style: style)
// Custom initialization
}
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(AddListObjectTableViewCell.classForCoder(), forCellReuseIdentifier: "AddCell")
}
@IBAction func editButtonPressed(editButton: UIBarButtonItem) {
if (self.tableView.editing) {
editButton.title = "Edit"
self.tableView.setEditing(false, animated: true)
} else {
editButton.title = "Done"
self.tableView.setEditing(true, animated: true)
}
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return listObjects.count + 1
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cellIdentifier = (indexPath.row < listObjects.count) ? "Cell" : "AddCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
if (indexPath.row < listObjects.count) {
let currentListObject : ListObject = listObjects[indexPath.row]
cell.textLabel.text = currentListObject.name
cell.detailTextLabel.text = currentListObject.detail
} else {
cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as AddListObjectTableViewCell
if (cell == nil) {
cell = AddListObjectTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
}
}
return cell
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
if (indexPath.row < listObjects.count) {
} else {
self.performSegueWithIdentifier("AddListObjectShow", sender: self)
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView?, canEditRowAtIndexPath indexPath: NSIndexPath?) -> Bool {
return (indexPath?.row < listObjects.count) ? true : false
}}
ho anche il seguente Swift mio UITableViewCell:
import UIKit
class AddListObjectTableViewCell: UITableViewCell {
@IBOutlet var addLabel : UILabel
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// Initialization code
}
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
}}
Infine, ecco una schermata del simulatore con la cella vuota visibile quando selezionato:
Ho ricontrollato tutte le mie prese di connessione, che i miei nomi di classe sono impostati correttamente in Interface Builder, ho registrato la classe di UITableViewCell con tableView e tutto sembra configurato correttamente. È possibile che questo sia un bug? Qualsiasi aiuto sarebbe molto apprezzato.
Il problema era che l'etichetta avevo aggiunto al sublass UITableViewCell non ha avuto la casella di controllo Installata selezionata per le dimensioni dello storyboard selezionato. Grazie. –
Alcuni dei miei vincoli AutoLayout non sono stati installati, quindi controllali anche tu! –