簡體   English   中英

具有多個部分的TableView快速

[英]TableView with multiple sections swift

我使用情節提要創建了一個包含兩個部分的表格視圖。 我希望能夠使用數組數組為每個部分顯示不同的項目,並能夠分別刪除每一行。 我嘗試了以下操作,但應用崩潰

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]'* **首先拋出調用堆棧:

當我有一個數組(例如字母數組)時,該應用程序運行正常,並且在兩個部分中都將顯示相同的數據

您可以嘗試刪除字母並替換數據,如下所示:

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
}

我發現了問題。 這是因為我的表視圖中的每個部分都有一行。 添加更多行后,該應用程序開始運行,並且沒有崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM