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