我遇到一个问题,其中eventsList在索引上有一个数字,我们将使用27作为示例,当我运行以下代码时,print(eventLatitudeString)和print(eventLongitudeString)行只打印一小部分他们两次。我是新手,请原谅这个烂摊子。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventsList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let path = Bundle.main.path(forResource: "Agencies", ofType: "plist"),
let agencyNameDict = NSDictionary(contentsOfFile: path) as? [String: String] {
translatedAgencyName = agencyNameDict[eventsList[indexPath.row].agencyId!]
}
let cell = myTableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ViewControllerTableViewCell
// create dateFormatter with UTC time format
utcDate = eventsList[indexPath.row].dateTime
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
dateFormatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone
let date = dateFormatter.date(from: utcDate!)
// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "MM-dd-yy HH:mm"
dateFormatter.timeZone = NSTimeZone(name: "EST")! as TimeZone
timeStamp = dateFormatter.string(from: date!)
cell.locationLabel.text = eventsList[indexPath.row].location
cell.eventTimeLabel.text = timeStamp
cell.eventTypeLabel.text = eventsList[indexPath.row].agencyEventTypeCode
cell.agencyIdLabel.text = translatedAgencyName
cell.eventTypeDescLabel.text = eventsList[indexPath.row].agencyEventTypeCodeDesc
let numOfEvents = eventsList.count
let stringNumOfEvents = "\(numOfEvents)"
numberOfEvents.text = stringNumOfEvents
eventLatitudeString = eventsList[indexPath.row].latitude!
eventLongitudeString = eventsList[indexPath.row].longitude!
print (eventLatitudeString)
print (eventLongitudeString)
如有必要,这是将项目附加到eventsList ...
的地方func fetchData(){
print ("entered fetch")
ref?.child("caddata").observe(.childAdded, with: { (snapshot) -> Void in
if let dictionary = snapshot.value as? [String: Any] {
let event = Events()
event.setValuesForKeys(dictionary)
if event.originatingAction == "CadEventNew" {
self.eventsList.insert(event, at: 0)
} else if event.originatingAction == "CadFieldEvent" {
self.eventsList.insert(event, at: 0)
} else if event.originatingAction == "CadEmergencyEvent" {
self.eventsList.insert(event, at: 0)
} else if event.originatingAction == "CadCloseEvent" {
self.cadPacketType = event.originatingAction
self.cadEventNumber = event.agencyEventId
self.queryCloseUIDFromDB()
} else {
// print ("The End")
}
DispatchQueue.main.async {
self.myTableView.reloadData()
}
}
})
}
答案 0 :(得分:0)
请确保
func numberOfSections(in tableView: UITableView) -> Int {
print("number of sections numberOfSections : \(tableView)")
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRowsInSections tableView : \(tableView)")
return eventsList.count
}
另外请检查所有索引路径被调用,jus通过打印indexPath,记住一件事。 cellForRowAt indexPath 仅在单元格显示时调用。如果单元格在tableview滚动的可见内容之外,则此方法不会调用。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("IndexPath - Section:\(indexPath.section) row:\(indexPath.row)")
if let path = Bundle.main.path(forResource: "Agencies", ofType: "plist"),
let agencyNameDict = NSDictionary(contentsOfFile: path) as? [String: String] {
.........................