【问题标题】:Empty cells in tableview表格视图中的空单元格
【发布时间】:2018-06-06 15:30:03
【问题描述】:

我正在 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
}

}

我在假定的函数中有一个打印语句来填充单元格,但它似乎永远不会到达那里。我以前做过tableview,但从来没有遇到过这个问题。单元格标识符在 Main.Storyboard 中也是相同的。

【问题讨论】:

  • tableView的数据源不是nil吗?
  • @AndreyChernukha 我不相信,它显示了包含 10 个单元格的 tableView,因为它应该来自文件。
  • 所以 cellForRowAt indexPath 被解雇了,对吧?你看到“在表格单元格的分配内部”打印了吗?
  • @AndreyChernukha 不,我不觉得这很奇怪,因为当我在另一个项目中创建不同的表时,我正在做完全相同的事情。
  • 在 numberOfRowsInSection 方法中放置一个断点。我想确保它真的被解雇了

标签: ios swift google-maps uitableview swift4


【解决方案1】:

问题是tableView的数据源为nil。只需这样做:

tableView.dataSource = self
tableView.delegate   = self

viewDidLoad 方法的最开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多