繁体   English   中英

在表视图单元格Swift iOS中检索数组的值

[英]Retrieving the values of array in table view cell swift ios

我正在尝试在表视图单元格中检索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)

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

YOUR_TABLE.reloadData()

并更新此方法,

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

numberOfRowsInSection方法中,返回wInstockArray数组计数

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

  return wInstockArray.count
}

在按钮单击动作内部,重新加载tableView

wInstockArray.append(item)
tableView.reloadData()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM