繁体   English   中英

UITableView的单元格重新排序-重新排序/编辑模式不显示(快速)

[英]UITableView Reordering Of Cells-Reorder/Edit Mode Not Displaying (Swift)

我在UIViewController有一个UITableView 我试图做到这一点,以便当用户单击NavigationBarBarButtonItem时,表BarButtonItem进入编辑模式,因此用户可以拖动并重新排列单元格。

但是,发生的是我只按了编辑按钮,什么也没有发生。

这是我尝试过的:

对于数组声明:

 var tester = ["1", "2", "3"]

对于按钮声明:

   @IBAction func editButtonPressed(sender: AnyObject) {
          self.editing = !self.editing
    }

对于各种tableview函数:

   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
    }

是什么导致此错误?

斯威夫特4.1

在→ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

添加此行以配置单元格:

cell.isEditing = self.tableView(tableView, canMoveRowAt: indexPath)

Ran Hassid在他的评论中对此有最正确的答案。 这将解决问题:

@IBOutlet weak var tableView: UITableView!
@IBAction func editButtonPressed(sender: AnyObject) {
    tableView.setEditing(!tableView.isEditing, animated: true)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM