Swift UITableViewCell加载但不显示
我正在尝试使用普通的Swift UITableViewCell加载但不显示,swift,uitableview,sdcalertview,sdcalertcontroller,Swift,Uitableview,Sdcalertview,Sdcalertcontroller,我正在尝试使用普通的UITableViewCell以编程方式将UITableView加载到自定义的AlertController中。UITableView显示,并且dataSource方法都会被调用,但是UITableViewCell不显示 代码如下: class TemplateTypeSelection : NSObject, UITableViewDelegate, UITableViewDataSource { var completion : (type:TemplateTy
UITableViewCell
以编程方式将UITableView
加载到自定义的AlertController
中。UITableView
显示,并且dataSource
方法都会被调用,但是UITableViewCell
不显示
代码如下:
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;
}
}
我检查了单元格的hidden
属性,并检查以确保tableView
在实例化和dataSource
方法中是相同的对象
我唯一的另一个线索是调用所有cellforrowatinexpath
方法后的控制台消息:
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
提前谢谢 尝试使用视图调试器。视图调试器说tableView中没有任何单元格。结果是cellForRowAtIndexPath正在大量生产空单元格。但是为什么dequeueReusableCellWithIdentifier给我一个空单元格?当你说“empty”时,这是什么意思?单元格本身不是nil,但是标签是0x0(不会被!unwrapper捕获)。如果你给标签设置了硬编码字符串,你会看到什么吗?另外,您确定您的表视图具有宽度吗?目前,您没有添加右侧约束。