我正在从SQLite
检索数据到表视图单元格,但数据被重复检索三次,意味着每个数据应该有一个单元格,检索到的数据将在其中。但目前的情况是,对于每个数据,三个单元格都在表格视图单元格中显示,如何在不重复的情况下检索数据。
这是代码:
import UIKit
import SQLite
import SQLite3
@available(iOS 11.0, *)
class ViewAssetViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var database: Connection!
let assetsTable = Table("AssetTable")
let assetid = Expression<Int>("assetid")
let assetname = Expression<String>("assetname")
let assetdescription = Expression<String>("assetdescription")
let assetform = Expression<String>("assetform")
let assetdate = Expression<String>("assetdate")
var assetIdArray = [Int]()
var assetNameArray = [String]()
var assetDescArray = [String]()
var assetTempArray = [String]()
var assetCreateDateArray = [String]()
var getAssetId = String()
var assetIdIs = Int()
var assetNameIs = String()
var assetDescIs = String()
var assetTempIs = String()
var assetCreateDateIs = String()
var SubmittedList = [String]()
@IBOutlet weak var menu: UIBarButtonItem!
@IBOutlet weak var SubmittedListTable: UITableView!
@IBAction func SubmittedLeftAct(_ sender: Any) {
print("submitted left action")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.getDataAssetIs()
return assetNameArray.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = SubmittedListTable.dequeueReusableCell(withIdentifier: "SubmittedCell") as! SubmittedTableViewCell
// self.getDataAssetIs()
cell.submittedLabel.text = assetNameArray[indexPath.row]
cell.submittedOnLabel.text = "Created on: \(assetCreateDateArray[indexPath.row])"
cell.associatedTempLabel.text = "Associated template: \(assetTempArray[indexPath.row])"
cell.assetDescLabel.text = assetDescArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
print("delete")
if editingStyle == .delete {
print("Deleted")
let userId = assetIdArray[indexPath.row]
let user = self.assetsTable.filter(self.assetid == userId)
let deleteUser = user.delete()
do {
try self.database.run(deleteUser)
// assetIdArray.remove(at: [indexPath.row])
assetIdArray.remove(at: indexPath.row)
assetNameArray.remove(at: indexPath.row)
assetDescArray.remove(at: indexPath.row)
assetTempArray.remove(at: indexPath.row)
assetCreateDateArray.remove(at: indexPath.row)
SubmittedListTable.deleteRows(at: [indexPath], with: .automatic)
// assetDescArray.remove(at: [indexPath.row])
// numbers.removeAtIndex(indexPath.row)
//tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
} catch {
print(error)
}
}
SubmittedListTable.reloadData()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// let indexPath = PDFListTable.indexPathForSelectedRow //optional, to get from any UIButton for example
// let currentCell = PDFListTable.cellForRow(at: indexPath!) as! PDFTableViewCell
// formName = currentCell.titleLabel!.text!
// print(currentCell.titleLabel!.text!)
self.moveToEdit()
}
func createAssetTable() {
print("CREATE TAPPED")
let createTable = self.assetsTable.create(ifNotExists: true){ (table) in
table.column(self.assetid, primaryKey: true)
table.column(self.assetname)
table.column(self.assetdescription)
table.column(self.assetform)
table.column(self.assetdate)
//try self.database?.run(usersTable.addColumn(formcreatedate))
}
do {
try self.database?.run(createTable)
print("Created Table")
} catch {
print(error)
}
}
func getDataAssetIs() {
do {
let users = try self.database?.prepare(self.assetsTable)
for user in users! {
print("userId: \(user[self.assetid]), name: \(user[self.assetname]), email: \(user[self.assetdescription]), formcate: \(user[self.assetform]), mydate: \(user[self.assetdate])")
assetIdIs = user[self.assetid]
assetNameIs = user[self.assetname]
assetDescIs = user[self.assetdescription]
assetTempIs = user[self.assetform]
assetCreateDateIs = user[self.assetdate]
self.assetIdArray.append(assetIdIs)
self.assetNameArray.append(assetNameIs)
self.assetDescArray.append(assetDescIs)
self.assetTempArray.append(assetTempIs)
self.assetCreateDateArray.append(assetCreateDateIs)
//print(formNameArray)
}
} catch {
print(error)
}
}
func moveToEdit() {
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewFormViewController") as! ViewFormViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
revealViewController().pushFrontViewController(newFrontViewController, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
do {
let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileUrl = documentDirectory.appendingPathComponent("Stephencelis").appendingPathExtension("sqlite3")
let database = try Connection(fileUrl.path)
self.database = database
} catch {
print(error)
}
self.createAssetTable()
/*
if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS AssetTable (assetid INTEGER PRIMARY KEY AUTOINCREMENT, assetname TEXT, assetdescription TEXT, assetform TEXT, assetdate Text)", nil, nil, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error creating table: \(errmsg)")
}
*/
SubmittedListTable.delegate = self
SubmittedListTable.dataSource = self
//SubmittedList = ["Asset title 1", "Asset title 2","Asset title 3","Asset title 4", "Asset title 5", "Asset title 6","Asset title 7", "Asset title 8", "Asset title 9","Asset title 10", "Asset title 11", "Asset title 12"]
// Do any additional setup after loading the view.
menu.target = revealViewController()
menu.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
navigationController?.navigationBar.barTintColor = UIColor(red:0.00, green:0.52, blue:1.00, alpha:1.0)
navigationController?.navigationBar.tintColor = UIColor.white
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
numberOfRowsInSection
可以被调用任意次。您不应该在该方法中加载数据。
拨打getDataAssetIs()
一次,而不是一遍又一遍。 viewDidLoad
将是一个称呼它的好地方。但绝对不是来自numberOfRowsInSection
。