在swift中从数据库获取数据时发生崩溃
我有一个表视图,我想在其中显示数据库中的所有数据。下面是代码在swift中从数据库获取数据时发生崩溃,swift,Swift,我有一个表视图,我想在其中显示数据库中的所有数据。下面是代码 但是它会崩溃。我认为它是数组->命令式的。。所以我必须像这样编写它 什么是崩溃日志?您可能有经典的索引越界错误。为什么您认为TableView中的行数将等于数据库表中的行数?崩溃时没有日志。。。。。从数据库中,该数组类似于。。。。。数组{User_Id=1;User_Name=sdfs;User_Password=sdfdsf;},{User_Id=3;User_Name=xyz;User_Password=1234;}arrayU
但是它会崩溃。我认为它是数组->命令式的。。所以我必须像这样编写它
什么是崩溃日志?您可能有经典的索引越界错误。为什么您认为TableView中的行数将等于数据库表中的行数?崩溃时没有日志。。。。。从数据库中,该数组类似于。。。。。数组{User_Id=1;User_Name=sdfs;User_Password=sdfdsf;},{User_Id=3;User_Name=xyz;User_Password=1234;}arrayUserData[indexPath.row]作为NSString将不起作用,因为正如您刚才向我们展示的,它不是字符串数组。它似乎是一组字典。所以抓取字典,然后从中提取用户名字段。顺便说一句,这是非常低效的,每次调用cellForRowAtIndexPath时都从表中检索所有行。那么我该怎么做呢。?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell = tableView.dequeueReusableCellWithIdentifier("cuntries") as? UITableViewCell
if cell == nil
{
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier:"cuntries")
}
var dbPath: NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
dbPath = dbPath .stringByAppendingPathComponent("UserDetail.sqlite")
var db = IMDDB()
var userinfo : UseInfoModel = UseInfoModel()
db.initWithPath(dbPath);
var arrayUserData = db.lookupAllForSQL("select * from UserData") as NSArray
cell?.textLabel.text = arrayUserData[indexPath.row] as NSString //Getting crash
cell?.detailTextLabel?.text = "hello"
cell?.setEditing(true, animated: true)
return cell!
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell = tableView.dequeueReusableCellWithIdentifier("UserData") as? UITableViewCell
if cell == nil
{
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier:"UserData")
}
var dbPath: NSString = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
dbPath = dbPath .stringByAppendingPathComponent("UserDetail.sqlite")
var db = IMDDB()
var userinfo : UseInfoModel = UseInfoModel()
db.initWithPath(dbPath);
var arrayUserData = db.lookupAllForSQL("select * from UserData") as NSArray
var arraydata = arrayUserData[indexPath.row] as NSMutableDictionary
cell?.textLabel.text = arraydata["User_Name"] as NSString
cell?.detailTextLabel?.text = arraydata["User_Password"] as NSString
cell?.setEditing(true, animated: true)
return cell!
}