在swift中从数据库中获取数据时出现崩溃

时间:2015-03-07 12:30:28

标签: swift

我有表格视图,我想在其中显示来自数据库的所有数据。这里有代码......

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!
}


但它会崩溃..

1 个答案:

答案 0 :(得分:0)

我认为它是数组 - > Dictonary ..所以我必须把它编码为..

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!
}