0
我正在開發使用Xcode 8和swift 3的IOS應用程序。我的應用程序有tableView中顯示的項目列表我想按日期搜索特定項目「using datePicker」。這些項目存儲在Core數據庫中。使用新數據搜索並重新載入TableView記錄
這是我的代碼。它搜索得很好,但我在顯示搜索數據時遇到了麻煩。
@IBAction func searchButton(_ sender: AnyObject) {
if(dateText.text != nil){
let request = NSFetchRequest <NSFetchRequestResult> (entityName : "Expense_Table")
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "expenseDate = %@", dateText.text!)
do {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! customeCell
let result = try self.context.fetch(request)
if (result.count > 0) {
for results in result as! [NSManagedObject]
{
first = results.value(forKey : "exoenseName") as! String
last = results.value(forKey : "expenseAmount") as! Double
second = results.value(forKey: "expenseDate") as! String
//print the data in the Output section of Xcode
print (second)
print (String (last))
print (first)
// save data in the cell of the tableView
cell.dateText.text = second
cell.amounttext.text = String (last)
cell.nameText.text = first
tableView.addSubview(cell)// this method print result over the table view data
}
tableView.reloadData()
}
}catch{}
}
}
我試試你的代碼,它顯示我的錯誤在(讓費用=支出[indexPath.row。此外,我有查詢,我用這種方法「cellForRowAt」顯示在Core數據庫中的所有數據,所以我怎麼能再次使用它來顯示搜索數據? –
錯誤是致命錯誤:索引超出範圍並且靠近代碼線程1:EXC_BAD_INSTRUCTION –
問題出在numberOfRowsInSection方法中,但我不知道如何解決它,請幫助我我是IOS的新開發人員app :( –