对此,Ran Hassid在他的评论中给出了最正确的答案。这将解决问题:
我在UIViewController
中有一个UITableView
。我试图让它在用户单击NavigationBar
中的BarButtonItem
时,表视图进入编辑模式,从而用户可以拖动并重新排列单元格。
然而,我只是简单地按下编辑按钮,什么也没发生。
这就是我尝试过的:
对于数组声明:
var tester = ["1", "2", "3"]
对于按钮声明:
@IBAction func editButtonPressed(sender: AnyObject) {
self.editing = !self.editing
}
对于各种表视图功能:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return tester.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("editCell", forIndexPath: indexPath) as! EditTableViewCell
cell.nameHolder?.text = tester[indexPath.row]
// Configure the cell...
return cell
}
func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
var itemToMove = tester[fromIndexPath.row]
tester.removeAtIndex(fromIndexPath.row)
tester.insert(itemToMove, atIndex: toIndexPath.row)
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
是什么原因导致了这个错误?
Swift 4.1
在→func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
添加此行以配置单元格:
cell.isEditing = self.tableView(tableView, canMoveRowAt: indexPath)
@IBOutlet weak var tableView: UITableView!
@IBAction func editButtonPressed(sender: AnyObject) {
tableView.setEditing(!tableView.isEditing, animated: true)
}