致命错误:swift2版本中的数组索引超出范围

时间:2017-07-17 09:19:41

标签: ios swift

请帮我解决错误 在这一行:

cell.name.text = names[indexPath.row] //error array  index out of range

这是我的声明:

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var names = ["Anna","aanal"]
    var petnames = ["Anu","aalu"]

    @IBOutlet var tableView: UITableView!

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {

            let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
            cell.name.text = names[indexPath.row] //error array  index out of range
            cell.petname.text = petnames[indexPath.row]
            return cell
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }        
    }

5 个答案:

答案 0 :(得分:4)

您的数组只有2个元素,而numberOfRowsInSection方法返回3.要么将其更改为2,要么在数组中添加3个元素

理想的方法是改变

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}

但通常不使用2个数组并创建一个名称和petname作为元素的Entity对象,然后有一个实体类的数组来填充tableview

答案 1 :(得分:0)

试试这个:

我认为您在petnames中传递了numberOfRowsInSection,名称和小号名称也不同,因此会出现此问题。

<强>解决方案

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if names.count == petnames.count{
            return petnames.count

        }
        return 0;
    }

您已经传递了更多numberOfRowsInSection然后传递了array.count

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2 or names.count
    }

答案 2 :(得分:0)

只需传递数组

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourArray.count
}

答案 3 :(得分:0)

数组namespetnames中有2个值。但是,表视图中有3行。因此,如果选择了表视图的第3行,则它将超出数组的范围,因为数组中没有第3个元素。

解决方案是让您的names数组和petnames数组包含3个元素:

var names = ["Anna","aanal", "boo"]
var petnames = ["Anu","aalu", "bo"]

或在表格视图中返回2行:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

最佳方式是返回数组的计数(如果两个数组的计数不同,则不返回):

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if names.count == petnames.count {
        return names.count
    }
    return 0
}

答案 4 :(得分:0)

var names = ["Anna","aanal"]  // you have 2 elemnts
var petnames = ["Anu","aalu"]

@IBOutlet var tableView: UITableView!

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return 3 // you have returned 3 but it should be 2

    // it should be
    return 2
    //or 
    //  return names.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
    cell.name.text = names[indexPath.row] //you got error because you are trying to get 3 element from array where as there are only 2 elements 
    cell.petname.text = petnames[indexPath.row]
    return cell
}

最好使用arrayname.count