在Swift中用SQLite数据库行填充UITableView

时间:2018-12-20 00:16:10

标签: ios swift sqlite uitableview

我试图说服我的应用程序遍历手机本地存储的SQLite数据库,然后将结果填充到单元格中。通过使用SQLite.swift

完成查询

我考虑过使用indexPath.row和SQL的LIMIT选项遍历单元格,但是我不知道我的方法是否正确。

到目前为止,我的代码:

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

    do {
        let query = fatherTable.limit(indexPath.row, offset: indexPath.row)
        for father in try self.localDatabase.prepare(query) {
            cell.label1.text = father[self.fatherName]
            cell.label2.text = father[self.fatherLastName]
            cell.label3.text = father[self.fatherAge]
        }
    }
    catch {
        print(error)
    }

    return cell
}

1 个答案:

答案 0 :(得分:0)

您的方法不正确,您需要获取viewDidLoad内的所有项目,然后重新加载表(如果您将以异步方式执行此操作),则在cellForRowAt内获取对性能是错误的,还要加上它将为最后一个项目设置单元格,因为每个单元格都调用cellForRowAt