Swift UITableViewController numberOfSections索引1超出静态单元格的界限[0..0]
我有一个Swift UITableViewController numberOfSections索引1超出静态单元格的界限[0..0],swift,uitableview,nsrangeexception,Swift,Uitableview,Nsrangeexception,我有一个UITableViewController,有5个静态部分,每个部分2行,但最后一个部分有1行 我已经在故事板中创建了表,并通过故事板设置了节数和行数。在协议函数中,我还设置了正确的行数和节数: override func numberOfSections(in tableView: UITableView) -> Int { return 5 } override func tableView(_ tableView: UITableView, numberOfRows
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.
}
*/
}
我只是有同样的问题,我花了一些时间来发现我的错误
我的错误是,故事板上仍然有一个单元格。在我删除单元和相应的部分/组后,它的工作方式应该是这样的。请确定,您是否在故事板中选择了静态单元?您可能还没有填充动态单元格。尽管我认为不可能用动态单元格创建分区。你能发布整个控制器吗?是的,我在故事板中选择了静态单元。现在发布整个视图控制器类。