首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Swift中使用开关/ case函数中的数组

在Swift中使用开关/ case函数中的数组
EN

Stack Overflow用户
提问于 2016-07-03 08:57:51
回答 1查看 1.1K关注 0票数 1

我正在尝试在我现有的应用程序中添加一个选择器视图。然而,我遇到了一个障碍。我仍然是Swift的新手,我不知道该如何描述这一点,但希望您能够充分理解下面的内容,提出适当的问题来解决这个问题。

我得到的错误消息是行中的“致命错误:超出范围的索引”:

代码语言:javascript
运行
复制
cell.txtField_PickData.text = self.arrayTemprature[indexPath.row].valueForKey("tempValue") as? String

myTitleArry等于两个输入“气温”或“水温”

下面是围绕此错误的代码部分。如果需要更多代码来帮助识别问题,则乐意提供:

代码语言:javascript
运行
复制
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
        }

    }

}

更新:我删除了标题数组,简单地添加了另一个动态单元格,并将驱动这两个单元格的数据分开。

标题的“超出范围的索引”消失,但现在为行创建相同的错误:

代码语言:javascript
运行
复制
 cell.txtField\_PickData.text = self.arrayTemprature[indexPath.row].valueForKey("tempValue") as? String

这个特性不需要额外的行,所以现在我想知道这个错误是否影响了数组收集数据的方式。

我已经添加了大部分代码。为了达到30k的限制,不得不减少一些。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-03 09:21:26

您的问题导致了错误的numberOfRowsInSection实现。

根据正式文件

告诉数据源返回表视图中给定部分中的行数。

请注意此方法,并进行所有必要的更改,以便为数据源带来所有可能的变体。

在这里您可以看到一个例子:

代码语言:javascript
运行
复制
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
    }
}

更新:(在对主要问题进行新编辑之后)

似乎有一个错误:第1节单元格有6个元素,但您必须声明:

代码语言:javascript
运行
复制
private let NumberOfRowsInSection1: Int                 = 5

有关您的问题的更多详细信息

你的问题是典型的:“指数超出范围”,这意味着什么?您的数组,在本例中,self.arrayTemprature没有语句期间所需的索引,因此,例如,如果您有一个由3个元素组成的数组,并且请求第四个元素,这就是您将看到的错误。但是,当您不初始化数组并假装请求一个不存在的元素时,也会发生此错误(您的数组为0,您希望始终使用第4个元素:索引超出范围)。所以,从声明、初始化、使用断点开始检查self.arrayTemprature,并检查为什么会出现此错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38168345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档