简体   繁体   中英

Empty cells in tableview

I am creating a tableview in swift 4 to display data read from a file. The table has the correct number of cells, but they are all empty. I am using an array of GMSMarker from GoogleMaps.

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
}

}

I have a print statement inside of the supposed function that is to populate the cells, but it seems like it never gets there. I have made a tableview before, but I have never had this problem. The cell identifier is the same in the Main.Storyboard one as well.

The problem is that the datasource of the tableView is nil. Simply do:

tableView.dataSource = self
tableView.delegate   = self

at the very beginning of viewDidLoad method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM