【问题标题】:Xcode crashing when displaying array in tableXcode 在表格中显示数组时崩溃
【发布时间】:2017-08-17 10:29:50
【问题描述】:

我来了

致命错误:索引超出范围”和“线程 1:EXC_BAD_INSTRUCTION (代码=EXC_I386_INVOP,子代码=0x0)”在“cell.textLabel?.text = 字符串(arrayTable[indexPath.row])

当我在操场上测试我的功能时,它按预期工作。我的数组被填充。所以,我不明白为什么 Xcode 在我的数组中找不到任何值。

有什么想法吗?

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var slider: UISlider!

@IBAction func sliderSelector(_ sender: Any) {
   arrayTable = [Int]()
    tableGenerate()
}

var arrayTable = [Int]()


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 50
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell")
    cell.textLabel?.text = String(arrayTable[indexPath.row])
    return cell
}

func tableGenerate () {
    var tableMultiplier = 1
    while tableMultiplier <= 50 {
        arrayTable.append(tableMultiplier * Int(slider.value))
        tableMultiplier += 1
    }

}

【问题讨论】:

  • fatal error: Index out of range 表示 numberOfRowsInSection 不与 cellForRowAtIndexPath 与您的数组计数同步。
  • 如下所示,将 return 50 更改为 return arrayTable.count 修复了崩溃

标签: arrays swift uitableview swift3


【解决方案1】:

试着做出这样的改变

 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayTable.count
    }

    func tableGenerate () {
        var tableMultiplier = 1
        while tableMultiplier <= 50 {
            arrayTable.append(tableMultiplier * Int(slider.value))
            tableMultiplier += 1
        }
      DispatchQueue.main.async {
         print(arrayTable)
         tblview.reloadData()
      }
    }

【讨论】:

  • 首先创建您的 tableview 对象,然后在此处重新加载它的工作,并检查您的 tableview 数据源或委托是否给定@Dev0urCode
  • 我写了你的建议,但我得到“对成员 'tableView(_:numberOfRowsInSection:)' 的模糊引用”我也确实将 tableGenerate 函数放在了 tableView 函数之后
  • youtube.com/watch?v=I4jSwWmqfOA 观看此视频并注意您做得不好的地方@Dev0urCode
  • 我在视频的任何地方都看不到任何 tableView.reloadData () :(
  • 只是看你是否将委托数据源方法提供给你的视图控制器或如何创建 tableview 的对象@Dev0urCode
【解决方案2】:

对于新手来说很常见的错误是从numberOfRowsInSection 返回任何幻数。取而代之的是,您应该返回您的真实物品数量:return arrayTable.count,我相信一切都应该正常工作。

【讨论】:

  • 但是在初始化数组后,tablview 没有更新,所以你应该重新加载它的数据
  • 它修复了崩溃但表格不显示数组,现在正在寻找,@DivyeshGondaliya 你知道如何重新加载吗?
  • 检查我的答案@Dev0urCode
  • 我做了,但很抱歉我不明白你的意思是在哪里重新加载?
  • @Dev0urCode 只需致电tableView.reloadData()
【解决方案3】:

当视图加载时,您的 tableView 将尝试返回 50 个单元格。对于您尝试将 textlabel 文本分配给的每个单元格:

cell.textLabel?.text = String(arrayTable[indexPath.row])

但由于在视图加载时 arrayTable 为空,因此应用程序在尝试通过 indexPath.row 访问索引时崩溃。

【讨论】:

    【解决方案4】:

    这个方法的问题

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourArray.count
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2017-09-26
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2011-12-03
      相关资源
      最近更新 更多