我正在 swift 4 中创建一个表视图来显示从文件中读取的数据。该表具有正确数量的单元格,但它们都是空的。我正在使用来自GoogleMaps的GMSMarker数组。
import UIKit
import GoogleMaps
class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var banner: UIImageView!
@IBOutlet weak var tableView: UITableView!
var arrayMarkers = [GMSMarker]()
override func viewDidLoad() {
super.viewDidLoad()
banner.image = #imageLiteral(resourceName: "Branding_Iron_Banner")
tableView.estimatedRowHeight = 155.0
tableView.rowHeight = UITableViewAutomaticDimension
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
let currentDate = Date()
print(formatter.string(from: currentDate))
guard let path = Bundle.main.path(forResource: "file", ofType: "txt") else {
print("File wasn't found")
return
}
guard let streamReader = StreamReader(path: path) else {
print("Dang! StreamReader couldn't be created!")
return
}
var lineCounter = 0
var lat = 0.0
var log = 0.0
var address = ""
var date = ""
var time = ""
var snip = ""
var snip2 = ""
var same = true
while !streamReader.atEof {
guard let nextLine = streamReader.nextLine() else {
print("Oops! Reached the end before printing!")
break
}
if(lineCounter % 5 == 0) {
lat = (nextLine as NSString).doubleValue
}
else if(lineCounter % 5 == 1) {
log = (nextLine as NSString).doubleValue
}
else if(lineCounter % 5 == 2) {
address = nextLine
}
else if(lineCounter % 5 == 3) {
date = nextLine
let fileDate = formatter.date(from: date)
if (fileDate?.compare(currentDate) == .orderedSame) {
snip2 = date
same = true
}
else if(fileDate?.compare(currentDate) == .orderedDescending) {
snip2 = date
same = true
}
else {
same = false
}
}
else if(lineCounter % 5 == 4){
if(same == true) {
time = nextLine
let position = CLLocationCoordinate2DMake(lat, log)
let marker = GMSMarker(position: position)
marker.title = address
snip = snip2 + "n"+time
marker.snippet = snip
arrayMarkers.append(marker)
print("n(String(describing: marker.title))")
}
}
lineCounter += 1
print("(lineCounter): (nextLine)")
}
print("The size of arrayMarkers: (arrayMarkers.count)")
self.title = "Number of entries: (arrayMarkers.count)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayMarkers.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!
print("Inside the assigning of table cells")
let marker = arrayMarkers[indexPath.row]
print(marker.snippet!)
cell.textLabel?.text = marker.title
cell.detailTextLabel?.text = marker.snippet
return cell
}
}
我在假定的函数中有一个 print 语句,用于填充单元格,但似乎它永远不会到达那里。我以前做过表格视图,但我从来没有遇到过这个问题。单元格标识符在 Main.Storyboard 中也是相同的。
问题是表视图的数据源为零。只需做:
tableView.dataSource = self
tableView.delegate = self
在viewDidLoad
方法的一开始。