我有一个UITableView,是在UIStoryboard中创建的,其中包含两个动态原型UITableViewCells:
如截图所示,第一个UITableViewCell的样式设置为Subtitle,第二个设置为自定义,并在中心添加了一个标签“Tap to Add”。第一个的标识符为“Cell”,第二个为“AddCell”。我已经设置了一个UITableViewController(我也尝试过UIViewController中的UITableView),用Swift编写了UITableViewCell子类,并连接了所有的outlets。但是,当我运行模拟器时,单元格被加载并且可点击,但我无法让它显示任何内容。(我尝试添加其他控件,但当单元格被加载时,没有任何东西会出现。唯一可以更改的是contentView的backgroundColor。)
下面是UITableViewController的Swift代码:
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
}}
我还有下面的Swift代码用于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
}}
最后,这是模拟器的截图,可以看到选中时空单元格为空:
我已经仔细检查了所有我的outlets是否连接正确,在Interface Builder中是否正确设置了类名,我已经将UITableViewCell的类注册到tableView中,并且一切都似乎配置正确。这可能是个bug吗?任何帮助将不胜感激。