我正在使用Xcode 6 beta 7.我有一个故事板,其中包含UIViewController,它有UITableview和UITableViewCell,所有内容都正确连接,我已经为UIViewController提供了所有必需的代码。
当我在模拟器中运行应用程序时,从不调用其中一个数据源函数“cellForRowAtIndexPath”,但调用“numberOfRowsInSection”
下面是我的视图控制器代码:
import UIKit
class DVViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var tableView : UITableView?
var tableData = ["Row 1", "Row 2", "Row 3"]
let kCellID = "cell"
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView?.delegate = self;
self.tableView?.dataSource = self;
//load cell
self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: kCellID);
self.tableView?.reloadData();
}
//MARK: Tableview delegates
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.tableData.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
//var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellID)
let cell : UITableViewCell = UITableViewCell (style: UITableViewCellStyle.Default, reuseIdentifier: kCellID);
return cell;
}
}
有人可以建议为什么从不调用“cellForRowAtIndexPath”,它曾经在Xcode6 beta 2中工作。
这是我做的,卸载Xcode 5和6只重新安装的Xcode 6,Ran项目和它工作。经过2天的调试后,重新安装完成了这个技巧。
我刚刚检查了你的代码,它确实有效。
检查numberOfRowsInSection
是否被调用。如果没有检查storyboard的连接。
试试这个:
class MainViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {
func viewDidLoad(){
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}