【发布时间】:2016-05-09 23:09:32
【问题描述】:
我使用情节提要创建了一个包含两个部分的表格视图。 我希望能够使用数组数组为每个部分显示不同的项目,并能够单独删除每一行。 我尝试了以下方法,但应用程序崩溃了
import Foundation
import UIKit
var letters: [String] = []
class NotificationsViewController: UITableViewController {
var data = [ letters, ["1","2","3"]]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.editing = true
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return letters.count
return data[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "tableCell")
// cell.textLabel?.text = letters[indexPath.row]
cell.textLabel?.text = data[indexPath.section][indexPath.row]
return cell
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// // Delete the row from the data source
// letters.removeAtIndex(indexPath.row)
//
// tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
data[indexPath.section].removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
}
这是我的 tableViewController: screeshot
错误:
2016-05-10 17:01:48.278 App[70893:6874230] * 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayI objectAtIndex:]:索引 1越界 [0 .. 0]' *** 首先抛出调用栈:
当我有一个数组(例如字母数组)时,应用程序可以正常工作,并且它将在两个部分中显示相同的数据
【问题讨论】:
-
您必须提供足够的信息让我们了解问题。当应用程序崩溃时,您看到的错误是什么?这是您的 TableViewController 的完整源代码吗?
-
是的,这是完整的来源。错误:2016-05-10 02:54:39.757 App[67795:6715726] *** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“ *** -[__NSArrayI objectAtIndex:]: 索引 1 超出范围 [0 .. 0]' *** 第一次抛出调用堆栈:
标签: ios iphone swift uitableview multidimensional-array