【问题标题】:Retrieving the values of array in table view cell swift ios检索表格视图单元格中的数组值swift ios
【发布时间】:2018-03-19 06:05:18
【问题描述】:

我正在尝试在表格视图单元格中检索 wInstockArray 的值。目前wInstockArray 是空的,我在单击按钮时插入一个字符串并附加到数组中。但是当我重试cellForRowAtIndexath 方法中的值时,它给出了错误

索引超出范围

我定义的是:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return wInstockArray.count + 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let firstCell = firstTableView.dequeueReusableCell(withIdentifier: "firstCell")! as UITableViewCell
    if wInstockArray.count == 0 {
        print("no widgets in instock")
    } else {
        firstCell.textLabel?.text = wInstockArray[indexPath.row]
        print("\(wInstockArray.count)")  //prints 1
        return firstCell
    }
}

内部按钮点击动作

wInstockArray.append(item)

【问题讨论】:

  • 请显示完整代码
  • 更新代码.......
  • @RajeshKumarR 更新
  • 因为你返回 wInstockArray.count + 1 它会得到 index out of range 。对于计数 +1,您应该返回空单元格

标签: ios swift uitableview nsarray


【解决方案1】:

内部按钮点击方法,请重新加载您的表格。

YOUR_TABLE.reloadData()

并更新此方法,

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

【讨论】:

    【解决方案2】:

    numberOfRowsInSection 方法中返回 wInstockArray 数组计数

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

    在按钮点击动作中,重新加载tableView

    wInstockArray.append(item)
    tableView.reloadData()
    

    【讨论】:

    • 它在 firstCell?.textLabel?.text = wInstockArray[indexPath.row] 处给出错误
    • 索引超出范围
    • @iOSDeveloper 你在 numberOfRowsInSection 方法中返回什么?
    • 道歉它的罚款
    猜你喜欢
    • 2018-07-27
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2015-03-07
    相关资源
    最近更新 更多