草庐IT

ios - 无需触摸即可滑动 UITableViewCell

coder 2023-07-15 原文

正如我的标题所说,当用户访问 ViewController 时,我想从左向右滑动 UITableView 的第一行。

在我的 ViewController 中,我有一个 UITableView,每一行都有两个按钮“更多”和“删除”操作。看下面的代码

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.delete) {
    }
}


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
        // Edit Button Action
    }
    deleteButton.backgroundColor = UIColor.red

    let editButton = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
        // Delete Button Action
    }
    editButton.backgroundColor = UIColor.lightGray

    return [deleteButton, editButton]
}

一切正常。但我希望当最终用户第一次访问此 ViewController 时,他们会通知有可用的滑动操作,以便他们执行它。

问题是:第一行如何自动左右滑动?

我做了什么?

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let cell = posTblView.cellForRow(at: NSIndexPath(row: 0, section: 0) as IndexPath) as! POSUserTabelCell
    UIView.animate(withDuration: 0.3, animations: {
        cell.transform = CGAffineTransform.identity.translatedBy(x: -150, y: 0)
    }) { (finished) in
        UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: {
            cell.transform = CGAffineTransform.identity
        }, completion: { (finished) in
        })
    }
}

通过上面的代码滑动/移动单元格是有效的,但不显示“删除”和“更多”按钮。

所以请指导我正确的方向。

最佳答案

我找到了一种实现它的方法。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if arrStudent.count > 0 {
        let indexPath = NSIndexPath(row: 0, section: 0)
        let cell = tblStudent.cellForRow(at: indexPath as IndexPath);

        let swipeView = UIView()
        swipeView.frame = CGRect(x: cell!.bounds.size.width, y: 0, width: 170, height: cell!.bounds.size.height)
        swipeView.backgroundColor = .clear

        let swipeEditLabel: UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 80, height: cell!.bounds.size.height))
        swipeEditLabel.text = "Edit";
        swipeEditLabel.textAlignment = .center
        swipeEditLabel.font = UIFont.systemFont(ofSize: 19)
        swipeEditLabel.backgroundColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 1) // Light Gray: Change color as you want
        swipeEditLabel.textColor = UIColor.white
        swipeView.addSubview(swipeEditLabel)

        let swipeDeleteLabel: UILabel = UILabel.init(frame: CGRect(x: swipeEditLabel.frame.size.width, y: 0, width: 90, height: cell!.bounds.size.height))
        swipeDeleteLabel.text = "Delete";
        swipeDeleteLabel.textAlignment = .center
        swipeDeleteLabel.font = UIFont.systemFont(ofSize: 19)
        swipeDeleteLabel.backgroundColor = UIColor(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red color: Change color as you want
        swipeDeleteLabel.textColor = UIColor.white
        swipeView.addSubview(swipeDeleteLabel)

        cell!.addSubview(swipeView)

        UIView.animate(withDuration: 0.60, animations: {
            cell!.frame = CGRect(x: cell!.frame.origin.x - 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 170, height: cell!.bounds.size.height)
        }) { (finished) in
            UIView.animate(withDuration: 0.60, animations: {
                cell!.frame = CGRect(x: cell!.frame.origin.x + 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 170, height: cell!.bounds.size.height)

            }, completion: { (finished) in
                for subview in swipeView.subviews {
                    subview.removeFromSuperview()
                }
                swipeView.removeFromSuperview()
            })

        }
    }
}

在单元格末尾添加自定义 UIView 并在动画完成后将其移除。

  • 您可以根据需要设置自定义 View 的框架
  • 根据需要添加带有背景颜色和标题的标签。

在这里你应该编写代码,而不是每次在 viewDidAppear 中调用此代码,它应该只调用一次以供用户指示。

关于ios - 无需触摸即可滑动 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45690519/

有关ios - 无需触摸即可滑动 UITableViewCell的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  4. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  5. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  6. ruby - 读取 zip 存档中的文件,无需解压缩存档 - 2

    我有一个包含100多个zip文件的目录,我需要读取zip文件中的文件以进行一些数据处理,而无需解压缩存档。是否有一个Ruby库可以在不解压缩文件的情况下读取zip存档中的文件内容?使用rubyzip报错:require'zip'Zip::File.open('my_zip.zip')do|zip_file|#Handleentriesonebyonezip_file.eachdo|entry|#Extracttofile/directory/symlinkputs"Extracting#{entry.name}"entry.extract('here')#Readintomemoryc

  7. ruby - 无需 eval 即时创建 Ruby 类 - 2

    我需要动态创建一个Ruby类,即动态地从ActiveRecord::Base派生。我暂时使用eval:eval%Q{class::#{klass}是否有一种等效的、至少同样简洁的方法可以在不使用eval的情况下执行此操作? 最佳答案 您可以使用Class类,其中的类是实例。困惑了吗?;)cls=Class.new(ActiveRecord::Base)doself.table_name=table_nameendcls.new 关于ruby-无需eval即时创建Ruby类,我们在Stac

  8. ruby - 现代计算机的功能是否不足以处理字符串而无需使用符号(在 Ruby 中) - 2

    我读过的关于Ruby符号的每一篇文章都在谈论符号相对于字符串的效率。但是,这不是1970年代。我的电脑可以处理一些额外的垃圾收集。我错了吗?我拥有最新最好的奔腾双核处理器和4GBRAM。我认为这应该足以处理一些字符串。 最佳答案 您的计算机可能能够处理“一点点额外的垃圾收集”,但是当“一点点”发生在运行数百万次的内部循环中时呢?如果它在内存有限的嵌入式系统上运行呢?有很多地方你可以随意使用字符串,但在某些地方你不能。这完全取决于上下文。 关于ruby-现代计算机的功能是否不足以处理字符串

  9. ruby-on-rails - ActionCable 无需升级到 Rails 5 beta - 2

    我目前有一个运行在4.2.5上的Rails应用程序,我想使用ActionCable而不必将整个应用程序升级到Rails5.0.0.beta3版本并冒破坏所有其他gem的风险。按照我在互联网上看到的指南,我已经尝试过gem'actioncable',github:'rails/actioncable'这不起作用,因为ActionCable存储库已合并到Rails存储库中。我什至试过gem'actioncable',github:'rails/rails'但这似乎不适用于ActionCable合并到Rails时发生的版本重新编号。(唯一低于5.0.0.beta*的版本是0.0.0,这似乎是

  10. ruby - 为 IO::popen 拯救 "command not found" - 2

    当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby​​1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#

随机推荐