0
函数cellForRowAtIndexPath即使在第一个视图或表条目后也没有被调用。这是待办事项列表应用程序的代码片段。cellForRowAtIndex路径未被调用
import UIKit
class FirstViewController: UIViewController , UITableViewDelegate , UITableViewDataSource {
@IBOutlet var tblTasks: UITableView!
let defaults = NSUserDefaults.standardUserDefaults()
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskMgr.tasks.count
}
**//THIS FUNCTION IS NOT BEING CALLED.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Check if Function is being called or not
println("hello")
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")
cell.textLabel!.text=taskMgr.tasks[indexPath.row].name
cell.detailTextLabel!.text=taskMgr.tasks[indexPath.row].name
return cell
}**
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if(editingStyle==UITableViewCellEditingStyle.Delete)
{
taskMgr.tasks.removeAtIndex(indexPath.row)
tblTasks.reloadData()
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println(taskMgr.tasks[indexPath.row])
}
override func viewDidLoad() {
super.viewDidLoad()
tblTasks.delegate = self
tblTasks.dataSource = self
tblTasks.reloadData()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
更重要的是nt问题是否调用'tableView:numberOfRowsInSection'。如果没有,你可能忽略了设置表视图的'dataSource'和'委托'。或者你忽略为IB中的整个场景设置基类。如果被调用,它返回什么值?如果它返回零,'cellForRowAtIndexPath'不会被调用。 – Rob
如果此数组为空,则调试并查找“taskMgr.tasks.count”的值,但它也不会被计数(因为您告诉tableview在tableview中没有行) – milo526
它正在被调用。 – shubhamr