Please help with the error I have on this line:
cell.name.text = names[indexPath.row] //error array index out of range
This is my declaration:
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.
}
}
namesandpetNames; this is risky, since if the arrays get out of sync you can have errors such as the one you have experienced. It is better to have a single array of Structs, each with anameand apetName