我是新的IOS开发人员,而且我正在使用Core数据库。我有一个包含3个不同属性的表(String,Date,Double)。我想将3个属性提取到一个单独的数组中。所以我可以完美地在表格视图中显示它们。 我正在使用Xcode 8和swift 3.
我尝试将它们放在数组中,如下所示,但我得到了错误:
Thread1:错误EXC_BAD_INSTRUCTION。另外,在输出窗口中显示 致命错误:索引超出范围
Sub Fill_Cell()
Dim rngCell As Range
For Each rngCell In Range("C7:K100")
If rngCell.Value = "0" Then
rngCell.Cells.Interior.ColorIndex = 3
Else
''If value = 0 & adjacent cell in column L <> ''
''rngCell.Cells.Interior.Pattern = xlGray16
End If
Next
End Sub
我该如何解决这个问题!!
答案 0 :(得分:0)
您实施tableview的方式是错误的。您应该从部分中的行数返回行数。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let request = NSFetchRequest <NSFetchRequestResult> (entityName : "EXPENSEAMOUNT_TABLE")
request.returnsObjectsAsFaults = false
do {
let result = try self.context.fetch(request)
if (result.count > 0) {
for index in 0 ..< result.count
{
let person = result[index] as! NSManagedObject
last [index] = person.value(forKey : "expenseAmount") as! Double //ERROR fires here
first [index] = person.value(forKey: "expenseAccountCode") as! String
}
}
} catch {
let fetchError = error as NSError
print(fetchError)
}
return result.count
}
然后从索引路径
的行的单元格返回单元格func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customeCell
cell.dateText.text = first[indexPath.row]
cell.amountText.text = String (last[indexPath.row])
return cell
}
答案 1 :(得分:0)
我尝试了其他方式将数据显示到tableView中,如下所示。 问题现在它将为我打印每次在原始表中的最后一条记录
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let request = NSFetchRequest <NSFetchRequestResult> (entityName : "EXPENSEAMOUNT_TABLE")
request.returnsObjectsAsFaults = false
var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customeCell
do {
let result = try self.context.fetch(request)
if (result.count > 0) {
for index in 0 ..< result.count
{
let person = result[index] as! NSManagedObject
if let first = person.value(forKey : "expenseAccountCode"),
let last = person.value(forKey : "expenseAmount") ,
let second = person.value(forKey: "expenseDate")
{
cell.dateText.text = String (describing: second)
cell.amountText.text = String (describing: last)
cell.nameText.text = String (describing: first)
}
}
}
}catch{ let fetchError = error as NSError
print(fetchError)}
return cell
}