【发布时间】: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