【问题标题】:Swift/iOS - UITableView with UITableViewCell not populating resultsSwift/iOS - 带有 UITableViewCell 的 UITableView 不填充结果
【发布时间】:2016-01-30 06:32:04
【问题描述】:

基本上,我正在尝试使用 SQLITE 结果获取我的 TableView,但它根本没有填充。奇怪的部分是我编辑了一个 sqlite 的示例项目来做同样的事情,但是从我的数据库而不是它工作。

另外,在我的项目中,除了单元格名称等之外,它的代码与 SQLITE 示例项目完全相同,我可以创建一个警报来显示数组中的一个结果,它工作正常。

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

对不起,如果我不能很好地解释它,但这是我的代码和一个尝试在没有电视结果但有警报的情况下工作的示例。

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

【问题讨论】:

  • 在尝试了多种方法之后,我使用 print("test1") 来查看它是否正在运行这些方法,并且似乎它们甚至没有被调用。我会尝试删除 TableView 和 Cell 并将它们重新添加到 ViewController。

标签: ios swift sqlite uitableview


【解决方案1】:

我通过在 ViewDidLoad() 中为我的 UITableView 提供委托和数据源的 self 方法来修复它。

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

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

【讨论】:

    【解决方案2】:

    如果您使用的是 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
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多