我一直在尝试和研究整整一天,仍然无法找到正确的答案。在按钮单击插入自定义UITableViewCell
我有一个按钮,我想在按钮点击时在我的静态UITableView中插入自定义的UITableViewCell。
这是我所做的。我使用自定义的UITableViewCell创建了一个xib文件,并为其分配了swift文件。
在我tableviewcontroller的viewDidLoad中,我注册了我的UITableViewCell:
override public func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "TableViewCell", bundle: nil)
addContactTableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
这是我为我的按钮点击功能:
@IBAction func buttonClick(sender: AnyObject) {
if let button = sender as? UIButton {
if let superview = button.superview {
if let cell = superview.superview as? UITableViewCell {
indexPath_g = self.tableView.indexPathForCell(cell)!
}
}
}
print(indexPath_g.row)
addContactTableView.beginUpdates()
addContactTableView.insertRowsAtIndexPaths([
NSIndexPath(forRow: indexPath_g.row, inSection: 1)
], withRowAnimation: .Automatic)
addContactTableView.endUpdates()
}
而且我的cellForRowAtIndexPath:
public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath_g) as! TableViewCell
if(indexPath.section == 1) {
//Configure the cell...
cell.detail_field.placeholder = "Phone"
}
return cell
}
我试图运行这个,我不断收到此错误:
fatal error: unexpectedly found nil while unwrapping an Optional value