我花了很多时间试图解决这个问题,但我没有快乐. 我有一个在我的appDelegate中创建的数组(levelArrayName),它由一个sqlite数据库填充: class AppDelegate: UIResponder, UIApplicationDelegate {var window: UIW
我有一个在我的appDelegate中创建的数组(levelArrayName),它由一个sqlite数据库填充:
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var database:COpaquePointer = nil var levelArray: [[Int]] = [] var levelArrayName: [[String]] = [] func checkdatabase(){ //check is database exist at path..... var dirPaths:NSString dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString; let databasePath = dirPaths.stringByAppendingString("/rugby.sqlite") let dbpath = databasePath.cStringUsingEncoding(NSUTF8StringEncoding) let filemanager = NSFileManager.defaultManager() if (!filemanager.fileExistsAtPath(databasePath)) { let fileForCopy = NSBundle.mainBundle().pathForResource("rugby",ofType:"sqlite") filemanager.copyItemAtPath(fileForCopy!, toPath:databasePath, error: nil) } let error = sqlite3_open(dbpath!, &database) if error != SQLITE_OK { println("error while opening"); sqlite3_close(database); } } func getLevels() { var selectQuery="SELECT * FROM level" var stmt:COpaquePointer = nil var result = sqlite3_prepare_v2(database, selectQuery, -1, &stmt, nil); if result != SQLITE_OK { println("failed \(sqlite3_errmsg(database))") println("failed \(sqlite3_errcode(database))") } else { result = sqlite3_step(stmt) while result == SQLITE_ROW { var levelID = Int(sqlite3_column_int(stmt, 0)) var attackID = Int(sqlite3_column_int(stmt, 2)) var defenceID = Int(sqlite3_column_int(stmt, 2)) var runs = Int(sqlite3_column_int(stmt, 3)) var tries = Int(sqlite3_column_int(stmt, 4)) var name_buf = sqlite3_column_text(stmt, 5) var name = String.fromCString(UnsafePointer<CChar>(name_buf)) result = sqlite3_step(stmt) self.levelArray += [[levelID, attackID, defenceID, runs, tries]] self.levelArrayName += [["\(name)"]] } } }
然后我在我的一个tableviewcontrollers中使用这个数组来设置单元格标签:
import Foundation import UIKit let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate class LevelsViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource { var levelArrayName = appDelegate.levelArrayName override func viewDidLoad() { super.viewDidLoad() self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return appDelegate.levelArray.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("LevelCell", forIndexPath: indexPath) as! UITableViewCell cell.textLabel?.text = levelArrayName[indexPath.row][0] return cell //return UITableViewCell() }
但是,单元格标签在数组中的字符串之前显示“Optional:”.
我已经尝试解开appDelegate.levelArrayName,但我似乎无法让它工作.
我敢肯定我错过了一些简单的东西,但是我希望能指出正确的方向.
谢谢
您需要在数组中的索引处解包该元素if let text = levelArrayName[indexPath.row][0] { cell.textLabel?.text = text }