我目前正在尝试从我的Child的文件夹中收到一个带有标题的图像数组到另一个通过detailViewController按钮连接的offertableview,但不幸的是我一直收到错误。下面我附上了我的firebase数据结构和我的主板截图的图像。
对于第一个表格视图,我有一个餐馆列表,在选择一个单元格后,它会转移到详细视图控制器,其中列出了餐厅的所有细节(为此我创建了我餐厅的模型)在那个detailVC我有一个连接到offertableview的按钮,列出了该特定餐厅的所有优惠。
当我点击按钮时,它会转移到商品表视图,这会导致应用程序因错误而关闭。
我提供的tableview代码:
var ref: DatabaseReference!
var offerImageArray = [String]()
var titleArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
fetchBars()
}
func fetchBars(){
ref.child("Paris").observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "offers")
let dict = imageSnap.value as! [String: Any]
let imageUrl = dict["offer_image"] as? String
let titleUrl = dict["offer_title"] as? String
self.offerImageArray = [imageUrl! as String]
self.titleArray = [titleUrl! as String]
}
})
self.tableView.reloadData()
}
覆盖func numberOfSections(在tableView:UITableView中) - > Int { 返回1 }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return offerImageArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OfferCell", for: indexPath) as! OffersTableViewCell
cell.offerImageView.sd_setImage(with: URL(string: self.offerImageArray[indexPath.row]))
cell.titleLabel.text = titleArray[indexPath.row]
return cell
}
Xcode错误:
2017-08-16 10:26:33.652申请[1174] [Firebase / Analytics] [I-ACS003007]已成功创建Firebase Analytics App会自动委派代理。要禁用代理,请设置 Info.plist中的标志FirebaseAppDelegateProxyEnabled为NO 2017-08-16 10:26:33.826申请[1174] [Firebase / Analytics] [I-ACS032003] iAd框架未链接。搜索 Ad Attribution Reporter已停用。 2017-08-16 10:26:33.828 申请[1174] [Firebase / Analytics] [I-ACS023012] Firebase 分析启用了致命错误:在展开时意外发现为零 一个可选值
答案 0 :(得分:0)
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
import SDWebImage
class OffersTableVC: UITableViewController {
var ref: DatabaseReference!
var offerImageArray = [String]()
var titleArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
fetchBars()
}
func fetchBars(){
Database.database().reference().child("paris").observeSingleEvent(of: .value, with: { (snapshot) in
print("Main Snapshot is \(snapshot)")
for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "offers")
if let snapDict = imageSnap.value as? [String:AnyObject] {
let dictKeys = [String](snapDict.keys)
print(dictKeys)
let dictValues = [AnyObject](snapDict.values)
print(dictValues)
for each in dictValues{
let imageUrl = each["offer_image"] as? String
print(imageUrl!)
self.offerImageArray.append(imageUrl!)
}
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.reloadData()
}
// let dict = imageSnap.value as! [String: Any]
// let imageUrl = dict["offer_image"] as? String
// let titleUrl = dict["offer_title"] as? String
// self.offerImageArray = [imageUrl! as String]
// self.titleArray = [titleUrl! as String]
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return offerImageArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OfferCell", for: indexPath) as! OffersTableViewCell
cell.offerImageView.sd_setImage(with: URL(string: self.offerImageArray[indexPath.row]))
// cell.titleLabel.text = titleArray[indexPath.row]
return cell
}
}
答案 1 :(得分:0)
1)在Appdelegate.swift中添加selectedBarname如下
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var selectedBarName = String()
2)MainTableVc添加以下代码
宣布上课后
let appDelegate = UIApplication.shared.delegate as! AppDelegate
在prepareForSegue
中if segue.identifier == "DetailView", let bar = selectedBar{
appDelegate.selectedBarName = bar.barName
3)OfferTableVc
现在只需调用此函数并完成,但现在不要将fetchBars调用为getOffers
func getOffers() {
let databaseRef = Database.database().reference().child("aktau")
databaseRef.queryOrdered(byChild: "bar_name").queryEqual(toValue: self.appDelegate.selectedBarName).observe(.value, with: { snapshot in
if ( snapshot.value is NSNull ) {
print("not found)")
} else {
print(snapshot.value!)
for child in snapshot.children {
let snap = child as! DataSnapshot
let imageSnap = snap.childSnapshot(forPath: "offers")
if let snapDict = imageSnap.value as? [String:AnyObject] {
let dictValues = [AnyObject](snapDict.values)
for each in dictValues{
let imageUrl = each["offer_image"] as? String
print(imageUrl!)
self.offerImageArray.append(imageUrl!)
}
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.reloadData()
}
}
}
})
}