jjzjj

ios - 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行

coder 2024-01-28 原文

may tableView 有一些奇怪的事情

我有

在我的代表中我有

var editingIndexPath: IndexPath?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if let editingIndexPath = editingIndexPath {
            return datasource.count + 1
        } else {
            return datasource.count
        }

    }

在我的 didSelect 中有

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

            if let oldEditingIndexPath = editingIndexPath {
                    self.editingIndexPath = nil
                    tableView.reloadData()
                    self.editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
                    tableView.insertRows(at: [self.editingIndexPath!], with: .top)
            } else {
                editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
                if let editingIndexPath = editingIndexPath {
                    tableView.insertRows(at: [editingIndexPath], with: .top)
                }
            }
    }

tableView.reloadData()报错后崩溃的问题

'attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update'

我不明白。 TableView 具有与执行插入所需的行数完全相同的行数。我将属性设置为 nil 然后重新加载表,通过此操作我将表的行数减少为两行。然后我再次将 editingIndexPath 设置为非 nil 值信号委托(delegate)方法,它应该使用 count + 1。但它以同样的方式失败。

同样有趣的是,同样的代码,但之前没有重新加载,永远不会失败

            editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
            if let editingIndexPath = editingIndexPath {
                tableView.insertRows(at: [editingIndexPath], with: .top)
            }

这里发生了什么?

最佳答案

TableView 的第 0 节(第 1 行、第 1 行、第 3 行)有 numberOfRowsInSection 3。在尝试插入超过 numberOfRowsInSection 计数的 Row4 时,抛出上述错误

这可能会更好(注意:无逻辑意图)

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     // better to check with datasource array elements count
     if indexPath.row + 1 < tableView.numberOfRows(inSection: indexPath.section) {
          self.editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
          tableView.insertRows(at: [self.editingIndexPath!], with: .top)
     } 
  }

关于ios - 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46975985/

有关ios - 尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. 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返回它复制的字节数,但是当我还没有下

  5. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

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

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

  7. 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使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  8. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

  9. ruby-on-rails - Rails Associations 的更新方法是什么? - 2

    这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user

  10. 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上

随机推荐