2016-07-03 10 views
1

Ich versuche, eine Picker-Ansicht zu meiner vorhandenen App hinzufügen. Ich habe jedoch einen Haken gefunden. Ich bin Swift immer noch neu, ich bin mir nicht sicher, wie ich das beschreiben soll, aber hoffentlich werden Sie genug von dem Folgenden verstehen, um die passende Frage zu stellen, um das zu lösen.Mit einem Array innerhalb einer Switch/Case-Funktion in Swift

Die Fehlermeldung I erhalten, ist "fatal error: Index außerhalb des Bereichs" aus der Leitung:

cell.txtField_PickData.text = self.arrayTemprature[indexPath.row].valueForKey("tempValue") as? String 

Die myTitleArry gleich zwei Eingänge "Lufttemperatur" oder "Wassertemperatur"

Hier ist der Teil des Codes um diesen Fehler herum. Wenn mehr Code benötigt wird, um das Problem zu identifizieren, zu schaffen glücklich:

import UIKit 

class DiveDetailsViewController: UITableViewController, LocationDelegate, ItemDataSelectedProtocol, UITextFieldDelegate , UIPickerViewDataSource , UIPickerViewDelegate 
{ 

let numberOfComponents: Int    = 2 
let temperatureComponentRows: Int  = 131 
let temperatureSymbolComponentRows: Int = 2 

let Fahrenheit: String     = "F" 
let Celsius: String      = "C" 
let minDegrees       = -10 
let maxDegrees       = 120 

private var degrees = [Int]() 

var temperature: Int     = 26 // our default temperature 
var temperatureType: String    = "C" // our default type is Farenheit 


// let myTitleArray = ["Air temperature" , "Water temperature"] 
var pickerView : UIPickerView! 
var pickerViewFarCel : UIPickerView! 


var dictTemprature = [String : String]() 

var arrayTemprature = [AnyObject]() 

var tempIndex = 0 
var tempSymbolIndex = 0 

var arraySymbol = ["C" , "F"] 

var tempratureOfAir : String = "" 
var tempratureOfWater : String = "" 



private typealias ItemDefaults = [ItemTypes : String] 

private let NumberOfSections: Int      = 7 
private let NumberOfRowsInSection0: Int     = 2 
private let NumberOfRowsInSection1: Int     = 7 
private let NumberOfRowsInSection2: Int     = 4 
private let NumberOfRowsInSection3: Int     = 6 
private let NumberOfRowsInSection4: Int     = 3 
private let NumberOfRowsInSection5: Int     = 4 
private let NumberOfRowsInSection6: Int     = 1 


// 
// Section 0 Cells 
// 
private let DiveNumberIndex: NSIndexPath    = NSIndexPath(forRow: 0, inSection: 0) 

private let DiveNameIndex: NSIndexPath     = NSIndexPath(forRow: 1, inSection: 0) 

// Section 1 Cells 

private let DiveWaterIndex: NSIndexPath     = NSIndexPath(forRow: 0, inSection: 1) 
private let DiveVisibilityIndex: NSIndexPath   = NSIndexPath(forRow: 1, inSection: 1) 
private let DiveCurrentsIndex: NSIndexPath    = NSIndexPath(forRow: 2, inSection: 1) 
private let AirTempPickerIndex: NSIndexPath    = NSIndexPath(forRow: 3, inSection: 1) 
private let WaterTempPickerIndex: NSIndexPath   = NSIndexPath(forRow: 4, inSection: 1) 
private let DiveWeatherIndex: NSIndexPath    = NSIndexPath(forRow: 5, inSection: 1) 
private let DiveSurfaceIndex: NSIndexPath    = NSIndexPath(forRow: 6, inSection: 1) 


// Section 2 Cells 

private let DiveLocationIndex: NSIndexPath    = NSIndexPath(forRow: 0, inSection: 2) 
private let DiveBodyOfWaterIndex: NSIndexPath   = NSIndexPath(forRow: 1, inSection: 2) 
private let DiveCityIndex: NSIndexPath     = NSIndexPath(forRow: 2, inSection: 2) 
private let DiveCountryIndex: NSIndexPath    = NSIndexPath(forRow: 3, inSection: 2) 

// Section 3 Cells 

private let DiveCircuitIndex: NSIndexPath    = NSIndexPath(forRow: 0, inSection: 3) 
private let DiveStartingPressureIndex: NSIndexPath  = NSIndexPath(forRow: 1, inSection: 3) 
private let DiveEndingPressureIndex: NSIndexPath  = NSIndexPath(forRow: 2, inSection: 3) 
private let DiveWeightIndex: NSIndexPath    = NSIndexPath(forRow: 3, inSection: 3) 
private let DiveDiveSuitIndex: NSIndexPath    = NSIndexPath(forRow: 4, inSection: 3) 
private let DiveEquipmentIndex: NSIndexPath    = NSIndexPath(forRow: 5, inSection: 3) 

// Section 4 Cells 

private let DiveEntryTypeIndex: NSIndexPath    = NSIndexPath(forRow: 0, inSection: 4) 
private let DiveDiveTypeIndex: NSIndexPath    = NSIndexPath(forRow: 1, inSection: 4) 
private let DiveRatingIndex: NSIndexPath    = NSIndexPath(forRow: 2, inSection: 4) 

// Section 5 Cells 

private let DiveDiveMasterIndex: NSIndexPath    = NSIndexPath(forRow: 0, inSection: 5) 
private let DiveDiveBoatOperatorIndex: NSIndexPath  = NSIndexPath(forRow: 1, inSection: 5) 
private let DiveDiveCenterIndex: NSIndexPath    = NSIndexPath(forRow: 2, inSection: 5) 
private let DiveTripOperatorIndex: NSIndexPath   = NSIndexPath(forRow: 3, inSection: 5) 

// Section 6 Cells 

private let DiveNotesIndex: NSIndexPath     = NSIndexPath(forRow: 0, inSection: 6) 



private let location: Location    = Location() 
private var isSelected: Bool    = false 
private var defaultValues: ItemDefaults  = ItemDefaults() 
private var selectedItemType: ItemTypes  = ItemTypes.None 
private var longitude: Double    = 0.0 
private var latitude: Double    = 0.0 


var diveModel: DiveModel = DiveModel() 

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    // Array of the Degree : 

    for i in self.minDegrees ..< self.maxDegrees+1{ 
     self.degrees.append(i) 
    } 
    print(self.degrees) 

    // Array of Table 

    self.dictTemprature = ["tempValue" : "" , "tempSymbol" : ""] 

    arrayTemprature = [self.dictTemprature , self.dictTemprature] 

    print(arrayTemprature) 

    print(self.arrayTemprature[0].valueForKey("tempValue")) 


    // 
    self.registerCustomTableViewCells() 

    self.defaultValues = self.getDefaultValues() 

    print(self.diveModel) 

} 

override func viewWillAppear(animated: Bool) 
{ 
    super.viewWillAppear(animated) 

    location.delegate = self 
    location.start() 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell: UITableViewCell! 

    switch indexPath 
    { 

    case DiveNumberIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveNumberCell) 
     (cell as! DiveNumberTableViewCell).textField.placeholder = Strings.DiveNumber.localized 
     //I realize this will be autoentered, but needs to be displayed 


    case DiveNameIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveSiteCell) 
     (cell as! DiveSiteTableViewCell).textField.placeholder = Strings.Name.localized 
     //This will need to be the data entered from the previous screen and not editable 


    case AirTempPickerIndex: 

     let cell : AirTemperatureTableViewCell = tableView.dequeueReusableCellWithIdentifier("AirTemperatureCell", forIndexPath: indexPath) as! AirTemperatureTableViewCell 
     cell.txtField_PickData.tag = indexPath.row 
     cell.textField_TempSymbol.tag = indexPath.row 

     cell.txtField_PickData.placeholder = "Air" 

     cell.textField_TempSymbol.placeholder = "" 
     cell.txtField_PickData.text = self.arrayTemprature[indexPath.row].valueForKey("tempValue") as? String 
     cell.textField_TempSymbol.text = self.arrayTemprature[indexPath.row].valueForKey("tempSymbol") as? String 
     return cell 


    case WaterTempPickerIndex: 

     let cell : WaterTemperatureTableViewCell = tableView.dequeueReusableCellWithIdentifier("WaterTemperatureCell", forIndexPath: indexPath) as! WaterTemperatureTableViewCell 
     cell.txtField_PickData.tag = indexPath.row 
     cell.textField_TempSymbol.tag = indexPath.row 

     cell.txtField_PickData.placeholder = "Water" 

     cell.textField_TempSymbol.placeholder = "" 
     cell.txtField_PickData.text = self.arrayTemprature[indexPath.row].valueForKey("tempValue") as? String 
     cell.textField_TempSymbol.text = self.arrayTemprature[indexPath.row].valueForKey("tempSymbol") as? String 
     return cell 


    case DiveLocationIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.LocationCell) 
     cell.textLabel!.text = Strings.Location.localized 
     cell.detailTextLabel!.text = String(format: "%f, %f", self.latitude, self.longitude) 

    case DiveWeatherIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Weather.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.Weather] 

    case DiveVisibilityIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Visibility.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.Visibility] 

    case DiveEntryTypeIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.EntryType.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.EntryType] 

    case DiveWaterIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Water.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.Water] 

    case DiveDiveSuitIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.DiveSuit.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.DiveSuit] 

    case DiveNotesIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveNoteCell) 

    case DiveRatingIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Rating.localized 
     cell.detailTextLabel!.text = "" 

    case DiveCurrentsIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Currents.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.Currents] 

    case DiveSurfaceIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Surface.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.Surface] 

    case DiveBodyOfWaterIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.BodyOfWater.localized 
     cell.detailTextLabel!.text = "" 

    case DiveCityIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.City.localized 
     cell.detailTextLabel!.text = "" 

    case DiveCountryIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Country.localized 
     cell.detailTextLabel!.text = "" 

    case DiveCircuitIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Circuit.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.Circuit] 

    case DiveStartingPressureIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.StartingTankUsageCell) 
     (cell as! StartingTankUsageCell).startingPressureTextField.placeholder = Strings.Start.localized 

    case DiveEndingPressureIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.EndingTankUsageCell) 
     (cell as! EndingTankUsageCell).textField.placeholder = Strings.Finish.localized 

    case DiveDiveMasterIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.DiveMaster.localized 
     cell.detailTextLabel!.text = "" 

    case DiveWeightIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.WeightsCell) 
     (cell as! WeightsTableViewCell).textField.placeholder = Strings.Weight.localized 

    case DiveEquipmentIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.Equipment.localized 
     cell.detailTextLabel!.text = "" 

    case DiveDiveTypeIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.DiveType.localized 
     cell.detailTextLabel!.text = self.defaultValues[ItemTypes.DiveType] 

    case DiveDiveBoatOperatorIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.BoatOperator.localized 
     cell.detailTextLabel!.text = "" 

    case DiveDiveCenterIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.DiveCenter.localized 
     cell.detailTextLabel!.text = "" 

    case DiveTripOperatorIndex: 
     cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveItemCell) 
     cell.textLabel!.text = Strings.TripOperator.localized 
     cell.detailTextLabel!.text = "" 


    default: 
     cell = nil 
    } 


    return cell 
} 


override func numberOfSectionsInTableView(tableView: UITableView) -> Int 
{ 
    return self.NumberOfSections 
} 


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    if section == 0 
    { 
     return self.NumberOfRowsInSection0 
    } 
    else if section == 1 
    { 
     return self.NumberOfRowsInSection1 
    } 
    else if section == 2 
    { 
     return self.NumberOfRowsInSection2 
    } 
    else if section == 3 
    { 
     return self.NumberOfRowsInSection3 
    } 
    else if section == 4 
    { 
     return self.NumberOfRowsInSection4 
    } 
    else if section == 5 
    { 
     return self.NumberOfRowsInSection5 
    } 
    else if section == 6 
    { 
     return self.NumberOfRowsInSection6 
    } 
    else 
    { 
     return 0 
    } 
} 



override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? 
{ 
    // 
    // It the row that is going to be selected is of an item type, then we save off the 
    // selectedItemType so it can be used during the segue. 
    // 
    switch indexPath 
    { 
    case DiveWeatherIndex: 
     self.selectedItemType = ItemTypes.Weather 

    case DiveVisibilityIndex: 
     self.selectedItemType = ItemTypes.Visibility 

    case DiveEntryTypeIndex: 
     self.selectedItemType = ItemTypes.EntryType 

    case DiveWaterIndex: 
     self.selectedItemType = ItemTypes.Water 

    case DiveDiveSuitIndex: 
     self.selectedItemType = ItemTypes.DiveSuit 

    case DiveDiveTypeIndex: 
     self.selectedItemType = ItemTypes.DiveType 

    case DiveCurrentsIndex: 
     self.selectedItemType = ItemTypes.Currents 

    case DiveSurfaceIndex: 
     self.selectedItemType = ItemTypes.Surface 

    case DiveCircuitIndex: 
     self.selectedItemType = ItemTypes.Circuit 

    default: 
     self.selectedItemType = ItemTypes.None 
    } 

    return indexPath 
} 



override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    if cell is DiveEditTableViewCell 
    { 
     (cell as! DiveEditTableViewCell).textField.userInteractionEnabled = true 
     (cell as! DiveEditTableViewCell).textField.becomeFirstResponder() 
    } 
    else if cell is DiveNoteTableViewCell 
    { 
     (cell as! DiveNoteTableViewCell).textView.userInteractionEnabled = true 
     (cell as! DiveNoteTableViewCell).textView.becomeFirstResponder() 
    } 
    else if cell is StartingTankUsageCell 
    { 
     (cell as! StartingTankUsageCell).startingPressureTextField.userInteractionEnabled = true 
     (cell as! StartingTankUsageCell).startingPressureTextField.becomeFirstResponder() 
    } 
    else if cell is EndingTankUsageCell 
    { 
     (cell as! EndingTankUsageCell).textField.userInteractionEnabled = true 
     (cell as! EndingTankUsageCell).textField.becomeFirstResponder() 
    } 
    else if cell is WeightsTableViewCell 
    { 
     (cell as! WeightsTableViewCell).textField.userInteractionEnabled = true 
     (cell as! WeightsTableViewCell).textField.becomeFirstResponder() 
    } 

} 

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) 
{ 

    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    if cell is DiveEditTableViewCell 
    { 
     (cell as! DiveEditTableViewCell).textField.userInteractionEnabled = false 
     (cell as! DiveEditTableViewCell).textField.resignFirstResponder() 
    } 
    else if cell is DiveNoteTableViewCell 
    { 
     (cell as! DiveNoteTableViewCell).textView.userInteractionEnabled = false 
     (cell as! DiveNoteTableViewCell).textView.resignFirstResponder() 
    } 
    else if cell is StartingTankUsageCell 
    { 
     (cell as! StartingTankUsageCell).startingPressureTextField.userInteractionEnabled = false 
     (cell as! StartingTankUsageCell).startingPressureTextField.becomeFirstResponder() 
    } 
    else if cell is EndingTankUsageCell 
    { 
     (cell as! EndingTankUsageCell).textField.userInteractionEnabled = false 
     (cell as! EndingTankUsageCell).textField.becomeFirstResponder() 
    } 
    else if cell is WeightsTableViewCell 
    { 
     (cell as! WeightsTableViewCell).textField.userInteractionEnabled = false 
     (cell as! WeightsTableViewCell).textField.becomeFirstResponder() 
    } 

} 


override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    var height = tableView.rowHeight 


    if indexPath == self.DiveNotesIndex 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier(Resource.DiveNoteCell) 
     height = CGFloat((cell?.bounds.size.height)!) 
    } 

    return height 
} 

func fahToCel(tempInF:Double) ->Double { 
    let celsius = (tempInF - 32.0) * (5.0/9.0) 
    return celsius as Double 
} 

func celToFah(tempInC:Double) ->Double { 
    let fahrenheit = (tempInC * 9.0/5.0) + 32.0 
    return fahrenheit as Double 
} 


func pickerFarCal(textField : UITextField){ 

    self.tempSymbolIndex = 0 

    pickerViewFarCel = UIPickerView(frame:CGRectMake(0, 0, self.view.frame.size.width, 216)) 
    pickerViewFarCel.delegate = self 
    pickerViewFarCel.dataSource = self 
    pickerViewFarCel.backgroundColor = UIColor.whiteColor() 
    textField.inputView = pickerViewFarCel 
    pickerViewFarCel.tag = textField.tag 

    let toolBar = UIToolbar() 
    toolBar.barStyle = .Default 
    toolBar.translucent = true 
    toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1) 
    toolBar.sizeToFit() 
    // Adds the buttons 

    let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(DiveDetailsViewController.doneClickSymbol)) 
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) 
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(DiveDetailsViewController.cancelClickSymbol)) 
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) 
    toolBar.userInteractionEnabled = true 
    textField.inputAccessoryView = toolBar 

} 

func doneClickSymbol(){ 

    self.view.endEditing(true) 

    print(self.arraySymbol[tempSymbolIndex]) 

    if self.arrayTemprature[pickerViewFarCel.tag].valueForKey("tempValue") as! String == ""{ 
     print("Not Convert") 
    }else if self.arrayTemprature[pickerViewFarCel.tag].valueForKey("tempSymbol") as! String == self.arraySymbol[tempSymbolIndex]{ 
     print("Not Convert") 
    }else{ 
     print("Convert") 

     if self.arraySymbol[tempSymbolIndex] == "C"{ 
      let value = Double(self.arrayTemprature[pickerViewFarCel.tag].valueForKey("tempValue") as! String) 
      let convertedValue = self.fahToCel(value!) 
      let myValue = String(format: "%.1f", convertedValue) 
      self.dictTemprature["tempValue"] = myValue 
      self.dictTemprature["tempSymbol"] = self.arraySymbol[tempSymbolIndex] 
     }else{ 
      let value = Double(self.arrayTemprature[pickerViewFarCel.tag].valueForKey("tempValue") as! String) 
      let convertedValue = self.celToFah(value!) 
      let myValue = String(format: "%.1f", convertedValue) 
      self.dictTemprature["tempValue"] = myValue 
      self.dictTemprature["tempSymbol"] = self.arraySymbol[tempSymbolIndex] 
     } 
     self.arrayTemprature[pickerViewFarCel.tag] = self.dictTemprature 

     tableView.reloadData() 

    } 

} 
func cancelClickSymbol(){ 
    self.view.endEditing(true) 
} 


// PickerView 

func pickerViewTemprature(textField : UITextField){ 

    // Index 

    self.tempSymbolIndex = 0 
    self.tempIndex = 276 

    pickerView = UIPickerView(frame:CGRectMake(0, 0, self.view.frame.size.width, 216)) 
    pickerView.delegate = self 
    pickerView.dataSource = self 
    pickerView.backgroundColor = UIColor.whiteColor() 
    textField.inputView = pickerView 
    pickerView.tag = textField.tag 
    pickerView.selectRow(276, inComponent: 0, animated: true) 


    let toolBar = UIToolbar() 
    toolBar.barStyle = .Default 
    toolBar.translucent = true 
    toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1) 
    toolBar.sizeToFit() 
    // Adds the buttons 


    let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: #selector(DiveDetailsViewController.doneClickMaterial)) 
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) 
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(DiveDetailsViewController.cancelClickMaterial)) 
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) 
    toolBar.userInteractionEnabled = true 
    textField.inputAccessoryView = toolBar 


} 

func doneClickMaterial(){ 

    self.view.endEditing(true) 

    self.dictTemprature["tempValue"] = "\(self.degrees[tempIndex])" 
    self.dictTemprature["tempSymbol"] = self.arraySymbol[tempSymbolIndex] 
    self.arrayTemprature[pickerView.tag] = self.dictTemprature 

    self.tableView.reloadData() 
} 
func cancelClickMaterial(){ 
    self.view.endEditing(true) 
} 

// MARK: delegate 

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { 
    if (pickerViewFarCel != nil){ 
     return 1 
    }else{ 
     return 2 
    } 

} 
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat{ 

    return 100 

} 
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 

    if (pickerViewFarCel != nil){ 
     return self.temperatureSymbolComponentRows 
    }else{ 
     if component == 0{ 
      return self.degrees.count 
     } 
     else{ 
      return self.temperatureSymbolComponentRows 
     } 

    } 

} 
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 

    if (pickerViewFarCel != nil){ 
     return self.arraySymbol[row] 
    }else{ 
     if component == 0 { 
      return "\(self.degrees[row])" 
     } else { 
      return self.arraySymbol[row] 
     } 
    } 
} 

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    if (pickerViewFarCel != nil){ 
     tempSymbolIndex = row 
    }else{ 
     if component == 0 { 
      tempIndex = row 
     } else { 
      tempSymbolIndex = row 
     } 

    } 

} 

UPDATE: Ich entfernte die Titel-Array und einfach hinzugefügt eine weitere dynamische Zelle und die Daten getrennt, die beiden antreibt.

Der Index außerhalb des zulässigen Bereichs verschwindet für den Titel, sondern schafft nun den gleichen Fehler für die Zeile:
cell.txtField_PickData.text = self.arrayTemprature [indexPath.row] .valueForKey ("tempValue"), wie? String

Diese Funktion erfordert keine zusätzliche Zeile, also frage ich mich, ob der Fehler in der Art und Weise auftritt, wie das Array seine Daten sammelt.

Ich habe den größten Teil des Codes hinzugefügt. Musste einige reduzieren, um die 30k Grenze zu erreichen.

+0

Das Problem ist einfach, dass 'myTitleArray' kein Element mit dem Wert 'indexPath.row' verknüpft hat.Sehen Sie sich die Zeilennummer an und sehen Sie sich an, wie viele Elemente Sie in diesem Array haben. Wenn dieses Array nur zwei Elemente hat, dann kann die Zeilennummer nur null oder eins sein (vorausgesetzt, dass diese Zeile gerade geschrieben wird). – Rob

+0

siehe update –

+0

Nochmals, um zu diagnostizieren, müssen Sie teilen (a) welcher Wert 'indexPath.row' war; und (b) was die 'Array-Temperatur' enthielt. Es scheint, dass "row" die Anzahl der Elemente in diesem Array überschritten hat. – Rob

Antwort

1

Ihr Problem wurde auf eine falsche numberOfRowsInSection Implementierung verursacht.

Nach dem official documentation:

Tells the data source to return the number of rows in a given section of a table view.

über diese Methode Achten Sie darauf, und alle Änderungen machen es müssen alle möglichen Variationen von Ihrer Datenquelle zu bringen.

Hier können Sie sehen, ein Beispiel:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if let count = self. myTitleArray?.count as Int { 
     return (count) 
    } else { 
     //myTitleArray is nil, so just return 0 
     return 0 
    } 
} 

aktualisieren: (nach der neuen Bearbeitung auf der Hauptfrage)

scheint es einen Fehler: Abschnitt 1 Zellen haben 6 Elemente aber Sie haben zu erklären:

private let NumberOfRowsInSection1: Int     = 5 

Mehr Details Über Ihr Problem:

Ihr Problem ist typisch: "Index außerhalb des Bereichs", was bedeutet das? Ihr Array, in diesem Fall self.arrayTemprature, hat nicht den Index, der während der Anweisung benötigt wird. Wenn Sie zum Beispiel ein Array von 3 Elementen haben und das vierte Element anfordern, ist dies der Fehler, den Sie sehen werden. Aber dieser Fehler ist auch passiert, wenn Sie Ihr Array nicht initialisieren und so tun, als ob Sie ein inexistentes Element anfordern (Ihr Array ist null und Sie wollen immer das vierte Element: index außerhalb des Bereichs). Überprüfe also deine self.arrayTemprature von Beginn der Deklaration, Initialisierung, benutze Breakpoint und überprüfe, warum du diesen Fehler bekommen hast.

+0

Alessandro, jetzt habe ich folgendes. Da es in diesem Abschnitt 7 Zeilen gibt, ist nicht sicher, wie das Obige implementiert werden soll. Ich habe zur Zeit: else if Abschnitt == 1 { return self.NumberOfRowsInSection1 } –

+0

Können Sie auf Ihre Frage sowohl numberOfRowInSection und numberOfSections Methoden hinzufügen? –

+0

Schau zu meinem Update .. –

Verwandte Themen