I'm trying to programmatically load a UITableView
into a custom AlertController
using a vanilla UITableViewCell
. The UITableView
displays and dataSource
methods all get called, but the UITableViewCell
does not display.
Here is the code:
class TemplateTypeSelection : NSObject, UITableViewDelegate, UITableViewDataSource {
var completion : (type:TemplateType)-> Void = { (type) -> Void in }
var alert : AlertController!
let myIdentifier = "MyReuseIdentifier";
func showDialog(completion:(type:TemplateType)-> Void) {
self.completion = completion
let alert = AlertController(title: "Select Template", message: nil)
let tableView = UITableView(frame: CGRectMake(0, 0, 240, 300), style: .Plain)
tableView.separatorInset = UIEdgeInsetsZero
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: myIdentifier)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = UIColor.clearColor()
alert.contentView.addSubview(tableView)
tableView.topAnchor.constraintEqualToAnchor(alert.contentView.topAnchor).active = true
tableView.bottomAnchor.constraintEqualToAnchor(alert.contentView.bottomAnchor).active = true
tableView.leftAnchor.constraintEqualToAnchor(alert.contentView.leftAnchor).active = true
alert.present()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.completion(type: TemplateType.CastAndCrew)
alert.dismiss()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(myIdentifier, forIndexPath: indexPath)
var name : String?
switch(indexPath.row) {
case 0 : name = TemplateType.CAPS.rawValue
case 1 : name = TemplateType.CastAndCrew.rawValue
case 2 : name = TemplateType.EASE.rawValue
case 3 : name = TemplateType.EP.rawValue
case 4 : name = TemplateType.Pixpay.rawValue
default : name = "Empty"
}
cell.textLabel!.text = name;
return cell;
}
}
I checked the hidden
property of the cell and I checked to make sure tableView
is the same object in the instantiation and the dataSource
methods.
The only other clue I have is this console message after all of the cellForRowAtIndexPath
methods are called:
2016-04-03 16:49:27.150 TimecardBuddy[18816:1378060] no index path for table cell being reused
2016-04-03 16:49:27.151 TimecardBuddy[18816:1378060] no index path for table cell being reused
2016-04-03 16:49:27.151 TimecardBuddy[18816:1378060] no index path for table cell being reused
2016-04-03 16:49:27.151 TimecardBuddy[18816:1378060] no index path for table cell being reused
2016-04-03 16:49:27.151 TimecardBuddy[18816:1378060] no index path for table cell being reused
Thanks in advance!