Swift / iOS - 带有UITableViewCell的UITableView无法填充结果

时间:2015-10-29 22:26:35

标签: ios swift sqlite uitableview

enter image description here基本上,我试图通过SQLITE结果获取我的TableView,但它根本没有流行。奇怪的部分是我编辑了一个sqlite的示例项目来做同样的事情,但是从我的数据库改为运行。

此外,在我的项目中,它具有与SQLITE示例项目完全相同的代码,除了单元名称等,我可以创建一个Alert来显示数组中的一个结果,并且它工作正常。

我不明白为什么它不会填充。

很抱歉,如果我无法解释清楚,但是这里是我的代码,以及它尝试使用没有电视结果但有警报的示例。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return returnedCardNames.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:CardInfo_Cell = tableView.dequeueReusableCellWithIdentifier("cardCell") as! CardInfo_Cell
    let card_name:CardInfo_Adapter = returnedCardNames.objectAtIndex(indexPath.row) as! CardInfo_Adapter
    cell.card_nameLabel.text = "\(card_name.CardName)"
    return cell
}

override func viewWillAppear(animated: Bool) {
    self.getCardNames()
}

func getCardNames()
{
    returnedCardNames = NSMutableArray()
    returnedCardNames = Card_Adapter.getInstance().getAllSimilarCardNamesByUserEntry("null", passedSetName: "null")
    cardResults.reloadData()

    let card_name:CardInfo_Adapter = returnedCardNames.objectAtIndex(10) as! CardInfo_Adapter
    Util.invokeAlertMethod("Card Results", strBody: card_name.CardName, delegate: nil)
}

enter image description here

2 个答案:

答案 0 :(得分:0)

我通过在ViewDidLoad()中为我的UITableView提供委托和dataSource的自我方法来修复它。

self.cardResults.delegate = self
self.cardResults.dataSource = self

我使用了另一个stackoverflow线程,它提供了答案。 UITableView datasource functions not called (swift)

答案 1 :(得分:0)

如果您正在使用libsqlite3库,请使用它(对我有用)

在didload()函数中创建数据库,然后在表视图中使用以下代码:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell        

    var selectStatement: OpaquePointer? = nil
    let selectSql = "select * from Inventory where ID = \((indexPath.row + 1))"
    if sqlite3_prepare_v2(Database.database.sqliteDB, selectSql, -1, &selectStatement, nil) == SQLITE_OK{
        if sqlite3_step(selectStatement) == SQLITE_ROW {

            let nameItem = sqlite3_column_text(selectStatement, 1)
            let price = sqlite3_column_double(selectStatement, 2)
            let quantity = sqlite3_column_int(selectStatement, 3)

            let nameString = String(cString: nameItem!)
            let priceString = String(format: "%.1f",price)
            let quantityString = String(quantity)

            cell.itemName.text = nameString
            cell.itemPrice.text = priceString
            cell.itemQuantity.text = quantityString

        }
    }

    return cell
}