cellForRowAtIndexPath不从自定义类中调用
我正在使用Xcode 7.0,Swift 2
我基本上是试图创build一个自定义的类,将build立一个UITable
,然后在ViewController
我做了一个新的对象的表,并加载到self.view;
我遇到的问题是函数func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
根本没有在自定义类中被调用。 我一直在寻找3天的解决scheme,我已经尝试重build应用程序和代码几次,没有运气。
请注意,如果我在ViewController.swift文件中使用相同的代码(这是build立表格所需的所有东西,不包括init函数等),它可以正常工作。
我知道问题是与cellForRowAtIndexPath
函数,因为它不会打印出我在代码块中设置的语句,当它运行。 所有其他函数被调用,但由于某种原因,这不被称为。 不知道我在这里忽略了什么。 任何帮助,将不胜感激。 提前致谢。
class sideTest: NSObject, UITableViewDelegate, UITableViewDataSource { let tesTable: UITableView = UITableView() var items: [String]? var mView: UIView = UIView() func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print("The number of rows is: \(self.items!.count)") return self.items!.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print("\nLets create some cells.") let sCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell! sCell.textLabel?.text = self.items![indexPath.row] sCell.textLabel?.textColor = UIColor.darkTextColor() return sCell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("You selected cell #\(indexPath.row)!") } func tblSetup() { self.tesTable.frame = CGRectMake(0, 0, 320, mView.bounds.height) self.tesTable.delegate = self self.tesTable.dataSource = self self.tesTable.backgroundColor = UIColor.cyanColor() // load cells self.tesTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") self.tesTable.reloadData() print("Currenlty in tblSetup.\nCurrent rows is: \(self.items!.count)") } //Init override init() { super.init() self.items = nil self.tblSetup() } init(sourceView: UIView , itemListAsArrayString: [String]) { super.init() self.items = itemListAsArrayString self.mView = sourceView self.tblSetup() } }
这是来自ViewController.swift的代码; 请注意,表格是内置的,但单元格不填充,即使我手动input单元格信息: sCell.textLabel?.text = "test cell"
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let myTable: sideTest = sideTest(sourceView: self.view, itemListAsArrayString: ["Cell 1", "Cell 2", "Cell 3"]) self.view.addSubview(myTable.tesTable) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
再次,任何帮助,不胜感激。 谢谢。
你的视图控制器没有一个强有力的参考你的sideTest变种。 一旦你的视图加载完成,你的sideTest是零。虽然你有一个tableview(通过添加子视图),但你不再有一个数据源。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}
在视图加载后调用。 这导致了问题。
将您的视图控制器更改为:
var tb :sideTest? override func viewDidLoad() { super.viewDidLoad() let myTable: sideTest = sideTest(sourceView: self.view, itemListAsArrayString: ["Cell 1", "Cell 2", "Cell 3"]) print(myTable.tesTable.frame) tb=myTable self.view.addSubview(myTable.tesTable) }
将您的cellforrowatindexpath更改为:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print("create cells") var cell :UITableViewCell? if let sCell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell"){ cell=sCell }else{ cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell") } cell!.textLabel?.text = self.items![indexPath.row] cell!.textLabel?.textColor = UIColor.darkTextColor() return cell! }
这将解决大部分问题。
你的代码:
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let myTable: sideTest = sideTest(sourceView: self.view, itemListAsArrayString: ["Cell 1", "Cell 2", "Cell 3"]) self.view.addSubview(myTable.tesTable) }
我会认为, myTable
variables超出了范围,并在viewDidLoad完成时释放,所以没有数据源或委托之后。 你确认了self.view.addSubview(myTable.tesTable)
保留了它? 尝试将myTable的声明移到function级别之外(到属性级别)或添加诊断打印以取消
- Swift – 从ConstUnsafePointer <()>转换
- 获取“重复接口定义”错误,肯定要#import ing头文件
- 如何计算FOV?
- Instagram API在400次错误的请求错误成功login后阻止redirect到iOS应用
- 在Today Widget Extension中经常出现“Unable to load”
- 检索didReceiveAuthenticationChallenge中的SSL证书字段
- 如何不断减lessImageViewanimation中的时间间隔
- 带POST的NSURLConnection不起作用
- PhoneGap InAppBrowser:打开iOS Safari浏览器