快速显示更多按钮时,取消在表格单元格上向左滑动

杰森·布雷迪(Jason Brady)

我想知道是否有适当的方法可以取消在表格视图单元格上向左滑动,从而使其向回滑动以隐藏按钮。我不太确定该如何正确地说,大声笑。但请参阅下面的GIF。在第一个GIF中,按下取消按钮后我没有代码,并且按钮保持可见状态。

在此处输入图片说明

我唯一的想法是用此代码重新加载单元格,self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)但这使按钮看起来向上移位,而我希望它看起来像是单元格向右移回原位。请参阅下面的重新加载GIF。

在此处输入图片说明

我应该如何正确地做到这一点?请参阅以下代码添加按钮及其功能的代码。

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let edit = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Edit") {
        (action, indexPath) in

        if (indexPath == self.indexSelect) {
            print("Editting Selected Cell")
        } else {
            print("Editting a difference cell than selected")
        }

        let section: Sections = self.frc.objectAtIndexPath(indexPath) as! Sections
        let count: Int = self.sectionCount(section.section!)
        var msg: String?
        let sectionName: String = section.section!

        if (count > 0) {
            msg = "There are \(count) other Items using this Section currently. Editing the Section name \"\(sectionName)\" will affect them all. \n\nThis will be changed immediately!"
        }

        let alert = UIAlertController(title: "Edit Section Name", message: msg, preferredStyle: UIAlertControllerStyle.Alert)
        let editAction = UIAlertAction(title: "Edit", style: UIAlertActionStyle.Destructive) {
            UIAlertAction in

            let sectionName = Util.trimSpaces(alert.textFields![0].text!)

            if (sectionName != section.section) {
                if (Util.checkSectionName(sectionName, moc: self.moc!) == false) {
                    let entityDesc = NSEntityDescription.entityForName("Sections", inManagedObjectContext: self.moc!)
                    let newSection: Sections = Sections(entity: entityDesc!, insertIntoManagedObjectContext: self.moc)
                    newSection.section = sectionName

                    do {
                        try self.moc!.save()
                    } catch {
                        fatalError("New item save failed")
                    }

                    let oldSection: Sections = section

                    let fetchReq = NSFetchRequest(entityName: "Catalog")
                    let pred = NSPredicate(format: "sections.section == %@", oldSection.section!)
                    fetchReq.predicate = pred

                    do {
                        let results = try self.moc!.executeFetchRequest(fetchReq)
                        for rec in results {
                            let catalog: Catalog = rec as! Catalog
                            catalog.sections = newSection
                        }

                        do {
                            try self.moc!.save()
                        } catch {
                            fatalError("Failed to Save after Delete")
                        }
                    } catch {
                        fatalError("Fetching Items to delete section failed")
                    }

                    self.moc!.deleteObject(oldSection)

                    do {
                        try self.moc!.save()
                    } catch {
                        fatalError("Failed to Save after Delete")
                    }
                } else {
                    Util.msgAlert("Duplicate Section Name", msg: "\"\(sectionName)\" section name already exists.", curVC: self)
                    self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
                }
            } else {
                self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
            }
        }
        let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
            UIAlertAction in

            //self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }

        alert.addAction(editAction)
        alert.addAction(cancel)
        alert.addTextFieldWithConfigurationHandler {
            (txtFld) -> Void in

            txtFld.text = section.section
            txtFld.autocapitalizationType = UITextAutocapitalizationType.Words
            txtFld.autocorrectionType = UITextAutocorrectionType.Default
            txtFld.clearButtonMode = UITextFieldViewMode.WhileEditing
        }

        self.presentViewController(alert, animated: true, completion: nil)
    }

    edit.backgroundColor = UIColor.init(red: 84/255, green: 200/255, blue: 214/255, alpha: 1)

    let delete = UITableViewRowAction(style: .Destructive, title: "Delete") {
        (action, indexPath) in

        let section: Sections = self.frc.objectAtIndexPath(indexPath) as! Sections
        let count: Int = self.sectionCount(section.section!)

        if (count > 0) {
            let alert = UIAlertController(title: "Confirm Delete", message: "There are \(count) Items using this Section currently. Deleting this Section will reset them all to blank. \n\nThis can't be undone and will take affect immediately!", preferredStyle: UIAlertControllerStyle.Alert)
            let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in }
            let deleteAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.Destructive) { UIAlertAction in
                var blankSection: Sections?

                var fetchReq = NSFetchRequest(entityName: "Sections")
                var pred = NSPredicate(format: "section == %@", "")
                fetchReq.predicate = pred

                do {
                    let results = try self.moc!.executeFetchRequest(fetchReq)
                    blankSection = (results.first as! Sections)
                } catch {
                    fatalError("Fetching blank section failed")
                }

                fetchReq = NSFetchRequest(entityName: "Catalog")
                pred = NSPredicate(format: "sections.section == %@", section.section!)
                fetchReq.predicate = pred

                do {
                    let group = try self.moc!.executeFetchRequest(fetchReq)
                    for rec in group {
                        let catalog: Catalog = rec as! Catalog
                        catalog.sections = blankSection
                    }

                } catch {
                    fatalError("Fetching Items to delete section failed")
                }

                self.moc!.deleteObject(section)

                do {
                    try self.moc!.save()
                } catch {
                    fatalError("Failed to Save after Delete")
                }

                if (self.sectionUpdateProtocol != nil) {
                    self.sectionUpdateProtocol!.sectionUpdate(self, section: blankSection!)
                }

                //self.navigationController!.popViewControllerAnimated(true)
            }

            alert.addAction(deleteAction)
            alert.addAction(cancel)

            self.presentViewController(alert, animated: true, completion: nil)
        } else {
            self.moc!.deleteObject(section)

            do {
                try self.moc!.save()
            } catch {
                fatalError("Failed to Save after Delete")
            }
        }

    }

    return [delete, edit]
}
wj2061

只需致电:

tableView.setEditing(false, animated: true)   

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当我在单元格上从右向左滑动时,UITableviewcell删除按钮会增加

来自分类Dev

向左滑动以选择CollectionView单元格(快速键5)

来自分类Dev

删除单元格时,表标题视图向左滑动

来自分类Dev

在表格视图单元格之间快速滑动?

来自分类Dev

AngularJs,显示按钮表格单元格

来自分类Dev

通过迭代创建的表格单元格是隐藏的-如何通过按钮取消显示每个条目?

来自分类Dev

在表格视图单元格中点击“阅读更多”按钮

来自分类Dev

如何快速显示和隐藏表格视图单元格

来自分类Dev

当用户在iOS中滑动时,如何在表格视图单元格中添加自定义按钮?

来自分类Dev

将按钮放在表格单元格上时崩溃,无法识别的选择器

来自分类Dev

将按钮放在表格单元格上时崩溃,无法识别的选择器

来自分类Dev

表格单元格上的按钮不起作用-迅速

来自分类Dev

选中单选按钮时更改表格单元格的颜色

来自分类Dev

快速反转表格单元格的顺序

来自分类Dev

在嵌套元素上显示表格单元格?

来自分类Dev

图像无法在表格单元格上完美显示

来自分类Dev

在嵌套元素上显示表格单元格?

来自分类Dev

在表格单元格上显示图像单击

来自分类Dev

打印时无法显示表格和单元格边框

来自分类Dev

在不可删除的行上滑动后取消选择单元格

来自分类Dev

滑动以在iOS中显示后尝试使表格单元格恢复其原始状态

来自分类Dev

表格单元格向左移动(swift3)

来自分类Dev

快速禁用TableView单元格按钮

来自分类Dev

在UITableIview中向后滑动表格视图单元格时设置操作

来自分类Dev

表格视图单元格中的按钮

来自分类Dev

单选按钮作为表格单元格

来自分类Dev

表格视图单元格内的按钮

来自分类Dev

滑动删除单元格不会取消UIButton操作

来自分类Dev

快速显示单元格中的文本

Related 相关文章

  1. 1

    当我在单元格上从右向左滑动时,UITableviewcell删除按钮会增加

  2. 2

    向左滑动以选择CollectionView单元格(快速键5)

  3. 3

    删除单元格时,表标题视图向左滑动

  4. 4

    在表格视图单元格之间快速滑动?

  5. 5

    AngularJs,显示按钮表格单元格

  6. 6

    通过迭代创建的表格单元格是隐藏的-如何通过按钮取消显示每个条目?

  7. 7

    在表格视图单元格中点击“阅读更多”按钮

  8. 8

    如何快速显示和隐藏表格视图单元格

  9. 9

    当用户在iOS中滑动时,如何在表格视图单元格中添加自定义按钮?

  10. 10

    将按钮放在表格单元格上时崩溃,无法识别的选择器

  11. 11

    将按钮放在表格单元格上时崩溃,无法识别的选择器

  12. 12

    表格单元格上的按钮不起作用-迅速

  13. 13

    选中单选按钮时更改表格单元格的颜色

  14. 14

    快速反转表格单元格的顺序

  15. 15

    在嵌套元素上显示表格单元格?

  16. 16

    图像无法在表格单元格上完美显示

  17. 17

    在嵌套元素上显示表格单元格?

  18. 18

    在表格单元格上显示图像单击

  19. 19

    打印时无法显示表格和单元格边框

  20. 20

    在不可删除的行上滑动后取消选择单元格

  21. 21

    滑动以在iOS中显示后尝试使表格单元格恢复其原始状态

  22. 22

    表格单元格向左移动(swift3)

  23. 23

    快速禁用TableView单元格按钮

  24. 24

    在UITableIview中向后滑动表格视图单元格时设置操作

  25. 25

    表格视图单元格中的按钮

  26. 26

    单选按钮作为表格单元格

  27. 27

    表格视图单元格内的按钮

  28. 28

    滑动删除单元格不会取消UIButton操作

  29. 29

    快速显示单元格中的文本

热门标签

归档