canMoveRowAtIndexPAth和canEditRowAtIndexPath方法问题

时间:2015-12-14 14:19:57

标签: swift methods tableview edit

当我构建它时,我有这个简单的应用程序完美无缺地工作,但后来我编辑了行,尝试在编辑模式下停止显示左侧的红色删除圈并且仅启用了滑动操作,但是移动行仍然在那里轻拍"移动"按钮。现在,我只剩下刷卡删除操作,移动根本不起作用,而且圈子也不再出现(这很好)。我把所有东西都还原成了以前的样子,但仍然没有效果。在xCode 7.2更新中有什么变化吗?或者我搞砸了一些东西,我不能再把手指放了? 以下是有问题的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    title = "Password Vault"
    navigationItem.prompt = "Add entry by clicking on the '+' sign -->"
    tableView.delegate = self
    tableView.dataSource = self


    let editButton = UIBarButtonItem(title: "Move", style: .Plain, target: self, action: "editRows")
    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "addEntry")

    navigationItem.rightBarButtonItems = [addButton, editButton]

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Lock", style: .Done, target: self, action: "lock")


    navigationController?.navigationBar.tintColor = UIColor.darkGrayColor() // for Bar Buttons


}
func editRows() {
    editing = true
    navigationItem.rightBarButtonItems![1] = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "finishEditingRows")
    navigationItem.rightBarButtonItems![0].enabled = false
}

func finishEditingRows() {
    editing = false
    navigationItem.rightBarButtonItems![1] = UIBarButtonItem(title: "Move", style: .Plain, target: self, action: "editRows")
    navigationItem.rightBarButtonItems![0].enabled = true
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.cellForRowAtIndexPath(indexPath)
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.cellForRowAtIndexPath(indexPath)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    let val = self.entries.removeAtIndex(sourceIndexPath.row)
    let val1 = self.usernames.removeAtIndex(sourceIndexPath.row)
    let val2 = self.passwords.removeAtIndex(sourceIndexPath.row)

    self.entries.insert(val, atIndex: destinationIndexPath.row)
    self.usernames.insert(val1, atIndex: destinationIndexPath.row)
    self.passwords.insert(val2, atIndex: destinationIndexPath.row)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return entries.count
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    tableView.cellForRowAtIndexPath(indexPath)
    return true
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    tableView.cellForRowAtIndexPath(indexPath)
    return true
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    cell.textLabel?.text = self.entries[indexPath.row]
    cell.textLabel?.textColor = UIColor.yellowColor()
    cell.textLabel?.font = UIFont(name: "TimesNewRomanPS-ItalicMT", size: 30)

    cell.accessoryType = .DetailButton
    cell.textLabel?.shadowColor = UIColor.grayColor()
    cell.textLabel?.shadowOffset = CGSize(width: 2, height: -2)


    return cell
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {


        entries.removeAtIndex(indexPath.row)
        usernames.removeAtIndex(indexPath.row)
        passwords.removeAtIndex(indexPath.row)

        //update entriesArray after deleting entries
        saveData()

        tableView.reloadData()

}

0 个答案:

没有答案