【发布时间】:2019-01-11 18:16:51
【问题描述】:
目前正在开发一个应用程序,该应用程序具有现有数组中所有项目的列表(位于单独的 .swift 文件中)。这是由 tableView 完成的。但是,我每次只得到一个空的 tableview。知道出了什么问题吗?
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
}
}
至于数组,我使用了结构体。结构体也会在 MKMapView 上被调用。
struct Location {
let title: String
let rating: String
let description: String
let latitude: Double
let longitude: Double
}
包含结构和数据的 .swift 文件:
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)
]
我在文件中使用var locationsAll = [Location]() 调用它
提前谢谢!
【问题讨论】:
-
你永远不会向你的数组中添加任何元素
locationsAll -
在单独的 .swift 文件中 ??
-
@Joakim Danielson 你到底是什么意思?该数组已经填充了我希望通过 tableView 显示的数据。
-
@Sh_Khan 数组,称为 Location。因为有几个控制器需要数组内的数据。
-
那个数组看起来不像填充了数据,你能告诉我们你是如何以及在哪里填充数据的吗?
标签: ios arrays swift uitableview