我有一个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.
}
*/
}
答案 0 :(得分:0)
我遇到了同样的问题,我花了一些时间才发现错误。
我的错误是故事板上还有一个单元格。在我删除了单元格和相应的部分/组之后,它应该如何工作。