Reputation: 1
I wrote this code, but when I try to execute it I have an error: "Fatal error: index out of range".
Here is my code:
import UIKit
class RestaurantPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var cell:[Bool] = []
@IBOutlet weak var cardTableView: UITableView!
let pictures: [UIImage] = [UIImage(named:"cinnamon lounge.jpg")!,UIImage(named:"thunapaha.jpg")!,UIImage(named:"Araliya.jpg")!,UIImage(named:"lakdevi.jpg")!,UIImage(named:"burdubai.jpg")!]
let titles: [String] = ["Cinnamon Loung Restaurant","Tunapaha Restaurant","Araliya Restaurant","Lak Dew Reastaurant"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ _tableView:UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}
func tableView(_ _tableView:UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = _tableView.dequeueReusableCell(withIdentifier: "cardCell", for: indexPath) as! CardCell
cell.configure(picture: pictures[indexPath.row], title: titles[indexPath.row])
return cell
}
}
How can I fix it?
Upvotes: 0
Views: 97
Reputation: 16341
Firstly create a model:
struct Restaurant {
var title: String
var picture: UIImage
}
Then use Restaurant
model in RestaurantPageViewController
array to populate tableView
.
class RestaurantPageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var cardTableView: UITableView!
let restaurants = [
Restaurant(title: "Cinnamon Loung Restaurant", picture: UIImage(named: "cinnamon lounge.jpg")!),
Restaurant(title: "Tunapaha Restaurant", picture: UIImage(named: "thunapaha.jpg")!),
Restaurant(title: "Araliya Restaurant", picture: UIImage(named:"Araliya.jpg")!),
Restaurant(title: "Lak Dew Reastaurant", picture: UIImage(named:"lakdevi.jpg")!)]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { restaurants.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cardCell", for: indexPath) as! CardCell
cell.configure(restaurant: restaurants[indexPath.row])
return cell
}
}
Finally, modify configure
method in CardCell
to configure your cell.
class CardCell: UITableViewCell {
func configure(restaurant: Restaurant) {
// do config
}
}
Upvotes: 1
Reputation: 710
you loaded 5 images into your pictures list and 4 titles into your titles list. When you access pictures[indexPath.row] and titles[indexPath.row], titles[indexPath.row] will throw an index out of range error when indexPath.row == 5 because you only have 4 string elements in 'titles'
tldr: add another title
Upvotes: 0
Reputation: 14397
You need to use Struct instead
struct MyModel {
let picture: UIImage?
let title: String?
}
Then add this struct to array
let model = MyModel(picture: UIImage(named:"cinnamon lounge.jpg"), title: "123")
var modelArray = [MyModel] ()
modelArray.append(model)
And use this modelArray in tableView ...
Upvotes: 0