Reputation: 85
Currently developing an app which has a list of all items in an existing array (which is in a separate .swift file). This is done by a tableView. However, I only get a empty tableview each time. Any idea what goes wrong?
import UIKit
import MapKit
//Initialize the TableViewController
class CategoriesController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//Retrieve the array
var locationsAll = [Location]()
//Outlet
@IBOutlet var tableView: UITableView!
//Load view
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locationsAll.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "allCell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "allCell")
let location = locationsAll[indexPath.row]
cell.textLabel?.text = location.title
cell.detailTextLabel?.text = location.rating
return cell
}
}
As for the array, I've used a struct. The struct is called on MKMapView as well.
struct Location {
let title: String
let rating: String
let description: String
let latitude: Double
let longitude: Double
}
The .swift file containing the struct & data:
struct Location {
let title: String
let rating: String
let description: String
let latitude: Double
let longitude: Double
}
let locations = [
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111)
]
I call it in the file using var locationsAll = [Location]()
Thank you in advance!
Upvotes: 1
Views: 7271
Reputation: 285260
It's discouraged to use a global array as data source. Data is supposed to be encapsulated in a struct, class, or enum.
You could create the locations inside the controller.
Replace
var locationsAll = [Location]()
with
var locationsAll = [
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111)
]
Or declare the locations as static variable in the struct
struct Location {
let title: String
let rating: String
let description: String
let latitude: Double
let longitude: Double
static let locations = [
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111),
Location(title: "something", rating: "", description: "Old.", latitude: 10.11111, longitude: 1.11111)
]
}
and populate the data source array
var locationsAll = Location.locations
Upvotes: 1
Reputation: 100541
Then you need
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "allCell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "allCell")
let location = locations[indexPath.row]
cell.textLabel?.text = location.title
cell.detailTextLabel?.text = location.rating
return cell
}
}
as it appears let locations = [
is a global var so it's accessible everywhere , or you can declare
var locationsAll = [Location]()
then in viewDidLoad
locationsAll = locations
Upvotes: 3
Reputation: 309
The count will equal 0. That’s why no cells are rendered. after adding data you need to relodData on the table view.
I assume you set up the cell in a storyboard? Otherwise you need to register the cell before using.
Upvotes: 0