Firebase 与 Swift 3.0 UITableView
Posted
技术标签:
【中文标题】Firebase 与 Swift 3.0 UITableView【英文标题】:Firebase with Swift 3.0 UITableView 【发布时间】:2016-09-23 06:02:01 【问题描述】:我搞砸了。我升级到 Swift 3.0 没有进行备份现在我的表格视图没有加载我知道它从 Firebase 中提取数据但表格视图没有加载。我在更新之前正在加载,但现在没有运气。
import UIKit
import Firebase
class ActiveCallsTableViewController: UITableViewController
let cellId = "callCell"
var users = [User]()
var unAcceptedUsers = [User]()
var enRoute = [User]()
var onSite = [User]()
var isFiltered = Bool()
let section = ["Unaccepted", "Enroute", "Onsite"]
override func viewDidLoad()
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
tableView.register(UserCell.self, forCellReuseIdentifier: cellId)
fetchUser()
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
print("titleForHeader")
return self.section[section]
func numberOfSectionsInTableView(tableView: UITableView) -> Int
return 3
/// Returns the amount of minutes from another date
func fetchUser()
print("This is fetchUser")
FIRDatabase.database().reference().child("Calls").observe(.childAdded, with: (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]
let user = User()
user.setValuesForKeys(dictionary)
self.users.append(user)
if user.Job_Status == "Unaccepted"
self.isFiltered = true
self.unAcceptedUsers.append(user)
print("\(user.Name) True")
else if user.Job_Status == "Enroute"
self.isFiltered = false
self.enRoute.append(user)
print("\(user.Name) False")
else
self.onSite.append(user)
//print(self.user)
//DispatchQueue.main.asynchronously(execute: self.tableView.reloadData())
DispatchQueue.main.async (execute: self.tableView.reloadData()
print("Later in fetchUser")
)
, withCancel: nil)
func handleCancel()
dismiss(animated: true, completion: nil)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
//var rowCount: Int
print(" This is numbers Of Rows")
if section == 0
//rowCount = unAcceptedUsers.count
print(unAcceptedUsers.count)
return unAcceptedUsers.count
else if section == 1
return enRoute.count
//rowCount = enRoute.count
else
return onSite.count
//return rowCount
func userForIndexPath(indexPath: NSIndexPath) -> User
if indexPath.section == 0
// print(unAcceptedUsers)
return unAcceptedUsers[indexPath.row]
print(enRoute)
return unAcceptedUsers[indexPath.row]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
//print("Call Cell")
//let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
print("CellForRow")
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath as IndexPath)
let user:User
//let user = userForIndexPath(indexPath)
if indexPath.section == 0
user = unAcceptedUsers[indexPath.row]
else if indexPath.section == 1
user = enRoute[indexPath.row]
else
user = onSite[indexPath.row]
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .short
let date3 = dateFormatter.date(from: user.Time_Logged!)
print("THE TIME IS \(date3)")
let elapesedTime = NSDate().timeIntervalSince(date3!)
//let minutesPassed = (elapesedTime / 3600)
//let dateMinutes = NSDate().timeIntervalSinceReferenceDate (date3)
// let calcendar = NSCalendar.currentCalendar()
//let dateComponents = calcendar.components(NSCalendarUnit.Minute, fromDate: date3!, toDate: NSDate(), options: nil)
//let minutesPassed = dateComponents
print ("THE TIME IS \(elapesedTime)")
let duration = Int(elapesedTime)
let minutesLogged = String(duration)
print(duration)
//let user = users[indexPath.row]
cell.textLabel?.text = user.Name
cell.detailTextLabel?.text = minutesLogged
//print(user.Adress)
return cell
//var valueToPass:String!
var valueToPass:String!
//var productsValue = [unAcceptedCallDataVie]]
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
let callInfoView:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Callinfo") as UIViewController
let unAcceptedView:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "unAcceptedCall") as UIViewController
let currentCell = tableView.cellForRow(at: indexPath as IndexPath) as UITableViewCell!;
if indexPath.section == 0
valueToPass = currentCell?.textLabel!.text
//valueToPass = currentCell2
performSegue(withIdentifier: "passData", sender: self)
self.present(unAcceptedView, animated: true, completion: nil)
//print(valueToPass)
else if indexPath.section == 1
self.present(callInfoView, animated: true, completion: nil)
print("Enroute")
else
self.present(callInfoView, animated: true, completion: nil)
print("Onsite")
// print(user.Adress)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if (segue.identifier == "passData")
//let user:User
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! unAcceptedCallDataView
// your new view controller should have property that will store passed value
//var passedValue = viewController.nameLable.text
//print(user.Adress)
viewController.LableText = valueToPass
class UserCell: UITableViewCell
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
`
【问题讨论】:
我发现了问题。当我从 Swift 2.2 迁移到 3.0 时,我只是让软件“修复”代码,但它做得很糟糕。我找到了正确的函数调用并替换了它。它有效,但我现在有 2 个不同的部分。 【参考方案1】:尝试替换:-
DispatchQueue.main.async (execute:...
收件人
DispatchQueue.global(qos: .background).async
self.tableView.reloadData()
DispatchQueue.main.async
self.tableView.reloadData()
【讨论】:
以上是关于Firebase 与 Swift 3.0 UITableView的主要内容,如果未能解决你的问题,请参考以下文章
Swift 3.0,Firebase:如何获取最后添加的记录
如何安装 Firebase Crashlytics Swift 3.0