我在swift 4中创建一个tableview来显示从文件中读取的数据。该表具有正确的单元格数,但它们都是空的。我正在使用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语句来填充单元格,但它似乎永远不会到达那里。我以前做了一个tableview,但我从未遇到过这个问题。单元标识符在Main.Storyboard中也是相同的。
答案 0 :(得分:0)
问题是tableView的数据源是零。只需:
tableView.dataSource = self
tableView.delegate = self
在viewDidLoad
方法的最开始。