无法调用 UITableViewDataSource 方法,调试时即使光标也无法进入这些方法
Posted
技术标签:
【中文标题】无法调用 UITableViewDataSource 方法,调试时即使光标也无法进入这些方法【英文标题】:UITableViewDataSource Method can not be called, Even cursor doesn't get inside those methods on debugging 【发布时间】:2016-07-30 07:43:46 【问题描述】:这是我的类,其中 UITableViewDatasource 的方法被覆盖,以便在单个单元格中使用 UITableViewCell 将数据作为“电子邮件”在代码中使用。但这些方法不会被执行。有人可以帮我解决这个问题吗?
导入 UIKit
var operationViewFlag: Int!
类 HomeCellView: UITableViewCell
@IBOutlet weak var btn_tablecell_Delete: UIButton!
@IBOutlet weak var btn_tablecell_Edit: UIButton!
@IBOutlet weak var lbl_tablecell_Email: UILabel!
ViewController 类:UIViewController、UITableViewDataSource、UITableViewDelegate
@IBOutlet weak var TableView_Mainscreen: UITableView!
@IBOutlet weak var lbl_MainScreen_Title: UILabel!
@IBOutlet weak var btn_Mainscreen_Insert: UIButton!
var databasepath:String!
var arrayStudInfo:NSMutableArray!
// viewDidLoad
override func viewDidLoad()
operationViewFlag=1
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lbl_MainScreen_Title.font = UIFont(name: "HelveticaNeue-Bold", size: 20)
btn_Mainscreen_Insert.layer.cornerRadius = 15
btn_Mainscreen_Insert.layer.borderWidth = 1
btn_Mainscreen_Insert.layer.borderColor = UIColor.brownColor().CGColor
// Variable Intialization
arrayStudInfo = NSMutableArray()
// Process for Creating Opening Database
let filemgr = NSFileManager.defaultManager()
let dirpath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docdir = dirpath[0] as String
databasepath = docdir.stringByAppendingString("StudentDataManipulation.sqlite");
if filemgr.fileExistsAtPath(databasepath as String)
let contactDB = FMDatabase(path: databasepath as String)
if contactDB == nil
print("Error: \(contactDB.lastErrorMessage())")
if contactDB.open()
let sql_stmt = "CREATE TABLE IF NOT EXISTS StudentData (Name TEXT, Phone TEXT, Email TEXT PRIMARY KEY NOT NULL, Comment TEXT)"
if !contactDB.executeStatements(sql_stmt)
print("Error: \(contactDB.lastErrorMessage())")
contactDB.close()
else
print("Error: \(contactDB.lastErrorMessage())")
//viewWillAppear
override func viewWillAppear(animated: Bool)
super.viewWillAppear(true)
listAllData()
//didReceiveMemoryWarning
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// Edit Method
@IBAction func btn_Edit_Clicked(sender: UIButton)
operationViewFlag=2
let updateView = self.storyboard?.instantiateViewControllerWithIdentifier("PerformOperation") as! PerformOperation
updateView.strEmail = arrayStudInfo[sender.tag]["Email"] as? String
print("Email to pass = \(updateView.strEmail)")
updateView.dictRecord = (arrayStudInfo[sender.tag] as! NSDictionary)
self.navigationController?.pushViewController(updateView, animated: true)
// Delete Method
@IBAction func btn_Delete_Clicked(sender: UIButton)
let contactDB = FMDatabase(path: databasepath as String)
let strDelete = arrayStudInfo[sender.tag]["Email"] as? String
if contactDB.open()
let deleteSQL = "DELETE FROM StudentData WHERE Email='\(strDelete!)'"
let result = contactDB.executeUpdate(deleteSQL, withArgumentsInArray: nil)
if !result
print("Error: \(contactDB.lastErrorMessage())")
else
print("Deleted Record.")
listAllData()
else
print("Error: \(contactDB.lastErrorMessage())")
contactDB.close()
// List All Data
func listAllData()
arrayStudInfo.removeAllObjects();
let contactDB = FMDatabase(path: databasepath as String)
if contactDB.open()
let filemgr = NSFileManager.defaultManager()
let dirpath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docdir = dirpath[0] as String
databasepath = docdir.stringByAppendingString("StudentDataManipulation.sqlite");
if filemgr.fileExistsAtPath(databasepath as String)
let contactDB = FMDatabase(path: databasepath as String)
if contactDB == nil
print("Error: \(contactDB.lastErrorMessage())")
if contactDB.open()
let sql_stmt = "CREATE TABLE IF NOT EXISTS StudentData (Name TEXT, Phone TEXT, Email TEXT PRIMARY KEY NOT NULL, Comment TEXT)"
if !contactDB.executeStatements(sql_stmt)
print("Error: \(contactDB.lastErrorMessage())")
let SelectQuery = "SELECT * FROM StudentData"
if let results:FMResultSet? = contactDB.executeQuery(SelectQuery, withArgumentsInArray: nil)
while results?.next() == true
let dictRecord = results?.resultDictionary()
arrayStudInfo.insertObject(dictRecord!, atIndex: arrayStudInfo.count)
print("\nRESULT : \(results?.resultDictionary())")
TableView_Mainscreen.reloadData()
else
print("Recored not found");
contactDB.close()
else
print("Error: \(contactDB.lastErrorMessage())")
contactDB.close()
else
print("Error: \(contactDB.lastErrorMessage())")
// TableView Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return arrayStudInfo.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let Cell = tableView.dequeueReusableCellWithIdentifier("CellHome") as! HomeCellView
Cell.lbl_tablecell_Email.text! = arrayStudInfo[(indexPath.row)]["Email"] as! String
print("Your Email : \(Cell.lbl_tablecell_Email.text!)")
Cell.btn_tablecell_Edit.tag=indexPath.row
Cell.btn_tablecell_Delete.tag=indexPath.row
Cell.btn_tablecell_Edit.layer.cornerRadius = 10
Cell.btn_tablecell_Delete.layer.cornerRadius = 10
// btn_Delete_Clicked
Cell.btn_tablecell_Edit.addTarget(self, action: #selector(ViewController.btn_Edit_Clicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
Cell.btn_tablecell_Delete.addTarget(self, action: #selector(ViewController.btn_Delete_Clicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
return Cell
【问题讨论】:
找不到任何行设置dataSource
of TableView_Mainscreen
。
非常感谢,一切顺利......!
【参考方案1】:
您是否将委托和数据源设置为 ViewController。
如果您使用情节提要,您可以控制点击您的 tableView 到 ViewController 并设置委托和数据源。
如果你想在代码中这样做,你可以这样做
TableView_Mainscreen.delegate = self
TableView_Mainscreen.datasource = self
此外,您应该使用小写字母来命名您的变量。
【讨论】:
以上是关于无法调用 UITableViewDataSource 方法,调试时即使光标也无法进入这些方法的主要内容,如果未能解决你的问题,请参考以下文章
Typescript中的“无法调用类型缺少调用签名的表达式”?