【问题标题】:TableView with multiple sections swift具有多个部分的 TableView 快速
【发布时间】: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


【解决方案1】:

您可以尝试如下删除字母和替换数据:

var  data = [["1","2","3"]]

如果你想要多个部分,像这样:

var  data = [["1","2","3"], ["4","5"]]

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return data.count
}

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 20
}

【讨论】:

    【解决方案2】:

    我发现了我的问题。 这是因为我的表格视图中的每个部分都有一行。一旦我添加了更多行,该应用程序就可以运行并且没有崩溃。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 2019-03-03
      • 2016-12-18
      • 2020-01-05
      相关资源
      最近更新 更多