【发布时间】:2015-04-15 20:02:37
【问题描述】:
我有一个带有自定义单元格类和导航控制器的表格。我向导航控制器添加了一个编辑按钮,并为视图控制器创建了一个 IBAction:
@IBAction func doEdit(sender: AnyObject) {
self.tableView.setEditing(true, animated: true)
}
Xcode 告诉我:
Cannot invoke 'setEditing' with an argument list of type '(Bool, animated: Bool)'
我也在 viewDidLoad 方法中尝试了navigationItem.rightBarButtonItem = editButtonItem(),但是那里没有显示删除图标。
编辑:我为 UITableView 创建了一个名为 table 的插座。当输入self.table.setEditing(true, animated: true) 时,上述错误不再出现,但应用程序在单击编辑按钮时崩溃。代码如下:
import UIKit
var arr = ["1", "2", "3"]
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var table: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCellClass = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCellClass
cell.cellTitle.text = arr[indexPath.row]
cell.backgroundColor = UIColor(red: 242.0/255.0, green: 157.0/255.0, blue: 48.0/255.0, alpha: 1.0)
return cell
}
@IBOutlet weak var editButton: UIBarButtonItem!
@IBAction func doEdit(sender: AnyObject) {
self.table.setEditing(true, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// navigationItem.rightBarButtonItem = editButtonItem()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
self.table.reloadData()
}
}
【问题讨论】:
-
您的“
tableView”是否作为IBOutlet连接到该视图控制器中的表视图? -
是的,它被称为“表”。请参阅上面的更新问题。
标签: ios uitableview swift