我正在查询解析以获取一些信息并在UITableViewCell中显示该信息,表中只有一个条目,带有虚构信息的测试条目,但是该表显示第一个单元格为空白,然后显示第二个单元格,其中包含该信息。我试图用谷歌搜索它,并且还添加了更多的测试信息行,但是无论哪种方式,该表在开始时都显示一个单元格而没有任何信息。这是我的代码:
var query = PFQuery(className: "marathons")
query.orderByAscending("end")
query.findObjectsInBackgroundWithBlock { (marathons, error:NSError?) -> Void in
if(error == nil ){
//success
for marathon in marathons! {
self.Name.append(marathon["Name"] as! String)
self.entryNumber.append(marathon["Number"] as! Int)
self.totalEntries.append(marathon["entries"] as! Int)
self.runnerDistance.append(marathon["distance"] as! Int)
}
self.TableView.reloadData()
}else {
print(error)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: TableView
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Name.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let singleCell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
singleCell.Name.text = names[indexPath.row]
singleCell.entryNumber.text = "\(entryNumbers[indexPath.row])"
singleCell.totalEntries.text = "\(entires[indexPath.row])"
singleCell.runnerDistance.text = "\(distance[indexPath.row])"
return singleCell
}
名称,entryNumber,totalEntries和RunnerDistance定义为:
@IBOutlet var Name: UILabel!
@IBOutlet var entryNumber: UILabel!
@IBOutlet var totalEntries: UILabel!
@IBOutlet var runnerDistance: UILabel!
有什么建议吗?我正在使用swift,解析作为我的后端以及XCODE 7
您的代码中定义的模型对象在哪里?要追加到命名对象Name
,entryNumber
,totalEntries
,和runnerDistance
,但你从命名属性尝试指派值entryNumbers
entries
和distance
。
编辑:我看到这里发生了什么。根据您的评论,您将像这样初始化数组:var names = [String()]
实际上创建了一个包含一个空字符串的数组[""]
。调用时append
,您要在数组中添加另一个元素,这使您的工作变得步入正轨["","John"]
。如果您想要一个空数组,则应该这样创建它:var names = [String]()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句