Ios numberOfRowsInSection未覆盖属性检查器
在Ios numberOfRowsInSection未覆盖属性检查器,ios,swift,uitableview,nsrangeexception,Ios,Swift,Uitableview,Nsrangeexception,在Swift 2中,使用将基于字典内容动态创建单元格的UITableView。这本词典有38个词条。当我在Attributes Inspector中将Table View部分手动设置为有38行时,一切都按预期进行 然而,这并不理想,我宁愿使用numberofrowsinssection来获得字典的计数,并以这种方式创建行/单元格。当Att检查器设置为1时,我得到一个越界错误 Terminating app due to uncaught exception 'NSRangeException',
Swift 2
中,使用将基于字典内容动态创建单元格的UITableView
。这本词典有38个词条。当我在Attributes Inspector中将Table View部分手动设置为有38行时,一切都按预期进行
然而,这并不理想,我宁愿使用numberofrowsinssection
来获得字典的计数,并以这种方式创建行/单元格。当Att检查器设置为1时,我得到一个越界错误
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
这是我的密码:
import SwiftyJSON
import UIKit
class OurTableViewController2: UITableViewController {
@IBOutlet weak var menuButton: UIBarButtonItem!
var labels = [String: UILabel]()
var strings = [String]()
var objects = [[String: String]]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
//gets data that will be used for cells
let urlString = "http://handheldart.org/api/tags/"
if let url = NSURL(string: urlString) {
if let data = try? NSData(contentsOfURL: url, options: []) {
let json = JSON(data: data)
parseJSON(json)
}
}
//menu button at top left of screen
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
//creates array 'objects'
func parseJSON(json: JSON) {
for result in json.arrayValue {
let id = result["id"].stringValue
let tagURL = result["url"].stringValue
let tagName = result["name"].stringValue
let obj = ["id": id, "tagURL": tagURL, "tagName": tagName]
//print (tagName)
objects.append(obj)
}
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
//create number of rows based on count of objects array
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print ("OBJECTS COUNT")
print (objects.count) //prints 38
return objects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
let object = objects[indexPath.row]
cell.textLabel?.text = object["tagName"]!
return cell
}
有什么建议吗 这是因为您将tableview的内容属性标记为“静态单元格”。您需要将tableview的内容设置为“动态原型”。您可以从tableview属性检查器->选择“第一个属性“内容”上的动态原型”来执行此操作
您不符合UITableViewDelegate和UITableViewDataSource的要求。如果您的数据都是动态的,为什么要尝试在情节提要中设置行数?我最初在那里创建单元格只是为了测试设计,现在正追溯到情节提要。这是一个简单的失误,只是忘记了将TableView设置为动态!