【发布时间】:2020-02-11 02:59:30
【问题描述】:
我对 swift 非常陌生,我试图了解使用 CoreData 填充 UIPickerView。在观看了有关使用 UITableView 执行此操作的视频后,我为 UIPickerView 实现了相同的代码。代码运行良好,直到我从 CoreData 中删除所有内容,然后它因错误索引超出范围而崩溃。
我知道它崩溃的代码行,但我知道如何修复它。我在这里阅读了许多关于索引超出范围的问题,但我看不到任何相同的内容。
这是代码,表格视图代码来自视频,我确实理解了 80%。
崩溃的代码行就在底部
import UIKit
import CoreData
// Global Variables and Constants
let appDelegate = UIApplication.shared.delegate as? AppDelegate
class ViewController: UIViewController {
// Outlets
@IBOutlet weak var tv: UITableView!
@IBOutlet weak var pickerView: UIPickerView!
// Varables
var taskArray = [Task]()
// Constants
let cellid = "CellId"
func randomNumber(num: Int) -> Int{
return Int(arc4random_uniform(UInt32(num)))
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
callDelegates()
}
override func viewWillAppear(_ animated: Bool) {
fetchyData()
tv.reloadData()
pickerView.reloadAllComponents()
}
func fetchyData(){
fetchData { (done) in
if done {
if taskArray.count > 0 {
tv.isHidden = false
pickerView.isHidden = false
} else {
tv.isHidden = true
pickerView.isHidden = true
}
}
}
}
func callDelegates(){
tv.delegate = self
tv.dataSource = self
tv.isHidden = true
pickerView.dataSource = self
pickerView.delegate = self
pickerView.isHidden = self
}
@IBAction func btnPressedButton(_ sender: Any) {
//textLbl.text = "\(items)"
pickerView.selectRow(randomNumber(num: 1000), inComponent: 0, animated: true)
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! TableViewCell
let task = taskArray[indexPath.row]
cell.taskLbl.text = task.taskDescription
if task.taskStatus == true {
cell.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
cell.taskLbl.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .none
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
self.deleteData(indexPath: indexPath)
self.fetchyData()
tableView.deleteRows(at: [indexPath], with: .automatic)
}
let taskStatusAction = UITableViewRowAction(style: .normal, title: "Completed") { (action, indexPath) in
self.updateTaskStatus(indexPath: indexPath)
self.fetchyData()
tableView.reloadRows(at: [indexPath], with: .automatic)
}
taskStatusAction.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)
deleteAction.backgroundColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
return [deleteAction, taskStatusAction]
}
}
extension ViewController {
func fetchData(completion: (_ complete: Bool) -> ()) {
guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Task")
do {
taskArray = try managedContext.fetch(request) as! [Task]
print("Data fetched, no issues")
completion(true)
} catch {
print("Unable to fetch data: ", error.localizedDescription)
completion(false)
}
}
func deleteData(indexPath: IndexPath) {
guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
managedContext.delete(taskArray[indexPath.row])
do {
try managedContext.save()
print("Data Deleted")
} catch {
print("Failed to delete data: ", error.localizedDescription)
}
}
func updateTaskStatus(indexPath: IndexPath) {
guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
let task = taskArray[indexPath.row]
if task.taskStatus == true {
task.taskStatus = false
} else {
task.taskStatus = true
}
do {
try managedContext.save()
print("Data updated")
} catch {
print("Failed to update data: ", error.localizedDescription)
}
}
}
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
self.fetchyData()
return taskArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
self.fetchyData()
return taskArray[row].taskDescription. //THE ERROR IS ON THIS LINE . Index Out OF Range//
}
}
【问题讨论】:
-
你的任务数组有多少个条目?
-
这行代码在做什么?它将尝试选择第 1000 行...如果 taskarray 中没有该数字...这将崩溃
-
那条线不是问题,我在按下按钮后将 cocoapod 用于无限的 UIpickerView,那是 1000,它工作正常。我的崩溃发生在线路上——返回 taskArray[row].taskDescription。仅当核心数据中没有任何内容时。当有数据时,它很好.. .
-
在此行之前添加一个检查...如果 taskArray[row] 存在或不存在
-
jawadAli,谢谢你,这就是我的问题,我相信它是一个 IF 语句,但我对 Swift 非常陌生(只有 1 个月)而且我卡住了,我写了不同的 if 语句,但它们不是在职的。能不能给我一个先机?
标签: ios swift uipickerview