Ios 错误:索引超出范围UITableView
我正在创建一个使用自定义单元格的应用程序。我还有这些Ios 错误:索引超出范围UITableView,ios,swift,uitableview,Ios,Swift,Uitableview,我正在创建一个使用自定义单元格的应用程序。我还有这些UITextView,如果您输入一个单词,那么该单词将转到我在自定义单元格中创建的四个标签之一。我仍然在编码它,但我得到了一个错误,说“错误:索引超出范围” 这是代码,我还评论了它给出错误的地方 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let c
UITextView
,如果您输入一个单词,那么该单词将转到我在自定义单元格中创建的四个标签之一。我仍然在编码它,但我得到了一个错误,说“错误:索引超出范围”
这是代码,我还评论了它给出错误的地方
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableView
cell.lbl.text = todolist[indexPath.row]
cell.lbl2.text = todolist2[indexPath.row] // This is the error code
cell.lbl3.text = todolist3[indexPath.row]
cell.lbl4.text = todolist4[indexPath.row]
return cell
}
这里是我附加文本的地方
@IBAction func ClickedforSelection(sender: AnyObject) {
todolist.append(txt.text!)
todolist2.append(txt1.text!)
todolist3.append(txt2.text!)
todolist4.append(txt3.text!)
self.view.endEditing(true)
txt.text = ""
txt1.text = ""
txt2.text = ""
txt3.text = ""
NSUserDefaults.standardUserDefaults().setObject(todolist, forKey: "list")
NSUserDefaults.standardUserDefaults().setObject(todolist2, forKey: "list2")
NSUserDefaults.standardUserDefaults().setObject(todolist3, forKey: "list3")
NSUserDefaults.standardUserDefaults().setObject(todolist4, forKey: "list4")
这是我的号码
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return todolist.count
}
我猜想这可能是对
indexPath.row
的重用。有解决方案吗?如果numberOfRowsInSection正在返回到olist.count,则您正在访问单元格中的olist 2。如果todolist有2个项目,而todolist2有1个项目,它将执行此操作,因为您试图访问不存在的列表中的项目。在cell.lbl.text的第一次调用处放置一个断点,并检查每个数组(todolist、todolist1等)。您应该看到todolist2在其调用的任何“行”上都没有记录。如果是这种情况,您应该在调用它之前测试它。(验证todolist2.count中是否有足够的项—或者更好的做法是,将代码更改为不包含跟踪一行的4个数组(转换为具有所有4个值的某种类型的结构,或者类似的内容)
首先,通过注释掉行来更改以下代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableView
cell.lbl.text = todolist[indexPath.row]
// cell.lbl2.text = todolist2[indexPath.row] // This is the error code
// cell.lbl3.text = todolist3[indexPath.row]
// cell.lbl4.text = todolist4[indexPath.row]
return cell
}
和测试以验证现有代码(应该可以工作,但它当然不会更新标签)
然后添加代码以打印每个数组中的项目数:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableView
cell.lbl.text = todolist[indexPath.row]
// cell.lbl2.text = todolist2[indexPath.row] // This is the error code
// cell.lbl3.text = todolist3[indexPath.row]
// cell.lbl4.text = todolist4[indexPath.row]
print("Row: \(indexPath.row)")
print("List 1: \(todolist.count)") //this will print to the console
print("List 2: \(todolist2.count)")
print("List 3: \(todolist3.count)")
print("List 4: \(todolist4.count)")
return cell
}
您可能会看到,它们的项目数不相同,一旦出现一个等于或大于项目数的“行”,它就会中断。请记住,该行从零开始,而计数从1开始
如果这是您发现的,那么将值添加到todolist数组的代码就有问题。如果您想了解如何将其转换为结构,我可以为您发布
转换为结构
单击某些内容时正在执行的代码:
@IBAction func ClickedforSelection(sender: AnyObject) {
显示每次向4个ToDoList中的每一个写入一个值。虽然我没有完整的要求,但如果这是您想要做的,那么您可以实现一个结构。将此代码放在它自己的ToDoList.swift文件中(理想情况下):
然后将定义todolislt阵列的位置(全部4个)替换为一个:
var listItems = [ToDoListItem]() //creates an array of ToDoListItems and initializes it with no values
然后在ClickedForSelection功能中,将其更改为:
let listItem = ToDoListItem(listItem: txt.text, list1Item: txt1.text, list2Item: txt2.text, list3Item: txt3.text)
listItems.append(listItem) //add it to your array
//todolist.append(txt.text!)
//todolist2.append(txt1.text!)
//todolist3.append(txt2.text!)
//todolist4.append(txt3.text!)
self.view.endEditing(true)
txt.text = ""
txt1.text = ""
txt2.text = ""
txt3.text = ""
// This routine will need to be updated. Leaving that for you to figure out :)
// NSUserDefaults.standardUserDefaults().setObject(todolist, forKey: "list")
// NSUserDefaults.standardUserDefaults().setObject(todolist2, forKey: "list2")
// NSUserDefaults.standardUserDefaults().setObject(todolist3, forKey: "list3")
// NSUserDefaults.standardUserDefaults().setObject(todolist4, forKey: "list4")
…然后numberOfRowsInSection更改为:
return listItems.count
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableView
let listItem = listItems[indexPath.row]
cell.lbl.text = listItem.listItem ?? "" // Since listItems.listItem is an optional value, ?? unwraps it safely. If it is nill, it uses "" instead
cell.lbl2.text = listItem.list1Item ?? ""
cell.lbl3.text = listItem.list2Item ?? ""
cell.lbl4.text = listItem.list3Item ?? ""
return cell
…然后cellForRowAtIndexPath更改为:
return listItems.count
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableView
let listItem = listItems[indexPath.row]
cell.lbl.text = listItem.listItem ?? "" // Since listItems.listItem is an optional value, ?? unwraps it safely. If it is nill, it uses "" instead
cell.lbl2.text = listItem.list1Item ?? ""
cell.lbl3.text = listItem.list2Item ?? ""
cell.lbl4.text = listItem.list3Item ?? ""
return cell
再次…我会强烈地考虑如何为每次4个列表中的ToLoistor存储一个值(如果它是一个待办事项列表应用程序,看起来这可能不是理想的?)
发布其他数据源方法以及如何追加文本将很有帮助。当然,请检查编辑器目前为止没有发现任何问题。可能您可以在每次尝试设置cell.lbl*.text时设置断点。它会告诉我“错误:索引超出范围”在cell.lbl2.text处,我是否应该将多个IndexPath.rows更改为其他内容?Todolist和Todolist 2有1项。我感觉需要更改“IndexPath.row”我不知道如何做。将代码添加到cellforrowatinedexpath
以打印indexPath.row
,todoList.count
和todoList2.count
。这应该会给您一个线索。对显示的每一行调用cellForRowAtIndexPath
,因此您必须捕获实际失败的调用…你能准确地键入我应该做什么吗?是的,我现在明白了。你能帮我把它转换成一个结构吗,如果你能的话我会很高兴的。你能发布一个结构吗