Table View Controller中的错误使用segue时出现致命错误:索引超出范围

时间:2019-02-17 15:29:36

标签: swift

我遇到了问题,无法解决

TableViewController:

var items: [String?] = ["door","table","chair"]
var givenDescription: [String?] = ["Change the description using 

class TableViewController: UITableViewController, UIToolbarDelegate {
the edit option"]
@IBOutlet var myTableView: UITableView!



override func viewDidAppear(_ animated: Bool) {
    myTableView.reloadData()
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return items.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    cell.textLabel?.text = items[indexPath.row]
    cell.detailTextLabel?.text = givenDescription[indexPath.row]

    return cell
}

ViewController:

import UIKit
import MobileCoreServices

class AddViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var newPic: Bool?

//Outlets
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var addName: UITextField!
@IBOutlet weak var addDescription: UITextField!
@IBAction func addImage(_ sender: Any) {
    let myAlert = UIAlertController(title: "Select Image From", message: "", preferredStyle: .actionSheet)
    let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: nil)
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.mediaTypes = [kUTTypeImage as String]
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
        self.newPic = true
    }
    let cameraRoll = UIAlertAction(title: "Camera Roll", style: .default, handler: nil)
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker.mediaTypes = [kUTTypeImage as String]
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
        self.newPic = false
    }
    myAlert.addAction(cameraAction)
    myAlert.addAction(cameraRoll)
    self.present(myAlert, animated: true)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    if mediaType.isEqual(to: kUTTypeImage as String) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image

        if newPic == true {
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageError), nil)
        }
    }
    self.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    self.dismiss(animated: true, completion: nil)
}

@objc func imageError(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer) {
    if error != nil {
        let alert = UIAlertController(title: "Save Failed", message: "Failed to save image", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "Okay", style: .cancel, handler: nil)
        alert.addAction(cancelAction)
        self.present(alert, animated: true, completion: nil)
    }
}

//Action
@IBAction func create(_ sender: Any) {
    if (addName.text != "") {
        items.append(addName.text!)
        addName.text = ""
    }
    if (addDescription.text != "") {
        givenDescription.append(addDescription.text!)
        addDescription.text = ""
    }
    self.navigationController?.popViewController(animated: true)
}   
}

2 个答案:

答案 0 :(得分:0)

您可以通过多种方式“修复” create函数,例如:

@IBAction func create(_ sender: Any) {
    if (addName.text != "" && addDescription.text != "") {
        items.append(addName.text!)
        addName.text = ""
        givenDescription.append(addDescription.text!)
        addDescription.text = ""
    }
    self.navigationController?.popViewController(animated: true)
}

但是更好的选择是创建一个简单的结构

struct Item {
    var name: String
    var itemDescription: String
}

并改用

@IBAction func create(_ sender: Any) {
    if (addName.text != "" && addDescription.text != "") { //or just check one depending on if one is more important
        let item = Item(name: addName.text!, itemDescription: addDescription.text!)
        items.append(item)
    }
    self.navigationController?.popViewController(animated: true)
}

items数组定义为

var items: [Item]()

并获取您的表格视图

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

    let item = items[indexPath.row]
    cell.textLabel?.text = item.name
    cell.detailTextLabel?.text = item.itemDescription

    return cell
}

答案 1 :(得分:0)

答案很简单。

python/mypy

}