![](/img/trans.png)
[英]UITableView Mix of Static and Dynamic sections Cells ? throw error -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
[英]Swift UITableViewController numberOfSections index 1 beyond bounds [0 .. 0] With Static Cells
我有一個UITableViewController
,具有靜態5個部分,每個部分2行,但最后一個部分有1行。
我已經在情節提要中創建了表格,並通過情節提要設置了節數和行數。 在協議功能中,我還設置了正確的行數和節數:
override func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 4) {
return 1
} else {
return 2
}
}
但是,我收到此錯誤:
'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
我相信此錯誤與numberOfSections
函數有關,因為如果我將其從5更改為0,它將運行良好。 當我在情節提要中將節的數組定義為5時,我不明白節的數組如何為零。是否有我沒有看到的簡單解決方案?
我還鏈接了委托源,並且我的數據源不是任何類型的數組,因為整個表是在情節提要中創建的,並且包含標簽等。
編輯
整個視圖控制器的代碼:
import UIKit
class RideSummaryTableViewController: UITableViewController {
@IBOutlet var nameLabel: UILabel!
@IBOutlet var ratingLabel: UILabel!
@IBOutlet var originStreetLabel: UILabel!
@IBOutlet var originCityLabel: UILabel!
@IBOutlet var destStreetLabel: UILabel!
@IBOutlet var destCityLabel: UILabel!
@IBOutlet var rateLabel: UILabel!
@IBOutlet var paymentButton: UIButton!
var paymentText = "Request Payment"
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.paymentButton.setTitle(paymentText, for: .normal)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 4) {
return 1
} else {
return 2
}
}
/*
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
return cell
}
*/
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
我只是遇到了同樣的問題,所以花了一些時間才能找到我的錯誤。
我的錯誤是故事板上還剩一個單元。 在刪除單元格和相應的部分/組之后,它應該如何工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.